Completed
Push — master ( 692aa1...25e5a7 )
by Paweł
60:22
created

ContentPushController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pushContentAction() 0 4 1
A getExistingArticleOrNull() 0 14 2
A findArticleByCodeAndOrganization() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Bundle\CoreBundle\Controller;
6
7
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
10
use SWP\Bundle\ContentBundle\Controller\ContentPushController as BaseContentPushController;
11
use SWP\Component\Bridge\Model\PackageInterface;
12
use Symfony\Component\HttpFoundation\Request;
13
14
class ContentPushController extends BaseContentPushController
15
{
16
    /**
17
     * Receives HTTP Push Request's payload which is then processed by the pipeline.
18
     *
19
     * @ApiDoc(
20
     *     resource=true,
21
     *     description="Adds a new content from HTTP Push",
22
     *     statusCodes={
23
     *         201="Returned on successful post."
24
     *     }
25
     * )
26
     * @Route("/api/{version}/content/push/", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_push")
27
     * @Method("POST")
28
     */
29
    public function pushContentAction(Request $request)
30
    {
31
        return parent::pushContentAction($request);
32
    }
33
34
    protected function getExistingArticleOrNull(PackageInterface $package)
35
    {
36
        $entityManager = $this->get('doctrine.orm.entity_manager');
37
        $entityManager->getFilters()->disable('tenantable');
38
        $existingArticle = $this->findArticleByCodeAndOrganization($package->getGuid());
39
40
        if (null === $existingArticle) {
41
            $existingArticle = $this->findArticleByCodeAndOrganization($package->getEvolvedFrom());
42
        }
43
44
        $entityManager->getFilters()->enable('tenantable');
45
46
        return $existingArticle;
47
    }
48
49
    private function findArticleByCodeAndOrganization(string $code = null)
50
    {
51
        $tenantContext = $this->get('swp_multi_tenancy.tenant_context');
52
53
        return $this->getArticleRepository()->findOneBy([
54
            'code' => $code,
55
            'organization' => $tenantContext->getTenant()->getOrganization(),
56
        ]);
57
    }
58
}
59