Completed
Push — master ( 9c95f5...e428dc )
by Paweł
06:06
created

findArticleByCodeAndOrganization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 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 SWP\Component\MultiTenancy\Model\OrganizationInterface;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class ContentPushController extends BaseContentPushController
16
{
17
    /**
18
     * Receives HTTP Push Request's payload which is then processed by the pipeline.
19
     *
20
     * @ApiDoc(
21
     *     resource=true,
22
     *     description="Adds a new content from HTTP Push",
23
     *     statusCodes={
24
     *         201="Returned on successful post."
25
     *     }
26
     * )
27
     * @Route("/api/{version}/content/push", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_push")
28
     * @Method("POST")
29
     */
30
    public function pushContentAction(Request $request)
31
    {
32
        return parent::pushContentAction($request);
33
    }
34
35
    protected function getExistingArticleOrNull(PackageInterface $package)
36
    {
37
        $entityManager = $this->get('doctrine.orm.entity_manager');
38
        $tenantContext = $this->get('swp_multi_tenancy.tenant_context');
39
        $organization = $tenantContext->getTenant()->getOrganization();
40
        $entityManager->getFilters()->disable('tenantable');
41
42
        $existingArticle = $this->findArticleByOrganizationAndCode($organization, $package->getGuid());
43
44
        if (null === $existingArticle) {
45
            $existingArticle = $this->findArticleByOrganizationAndCode($organization, $package->getEvolvedFrom());
46
        }
47
48
        $entityManager->getFilters()->enable('tenantable')
49
            ->setParameter('tenantCode', $tenantContext->getTenant()->getCode());
50
51
        return $existingArticle;
52
    }
53
54
    private function findArticleByOrganizationAndCode(OrganizationInterface $organization, string $code = null)
55
    {
56
        return $this->getArticleRepository()->findOneBy([
57
            'code' => $code,
58
            'organization' => $organization,
59
        ]);
60
    }
61
}
62