ArticleManager::updateSettings()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 39
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 2
nop 1
1
<?php
2
3
namespace Victoire\Bundle\BlogBundle\Manager;
4
5
use Doctrine\ORM\EntityManager;
6
use Victoire\Bundle\BlogBundle\Entity\Article;
7
use Victoire\Bundle\BlogBundle\Entity\Tag;
8
use Victoire\Bundle\BusinessPageBundle\Builder\BusinessPageBuilder;
9
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
10
use Victoire\Bundle\BusinessPageBundle\Transformer\VirtualToBusinessPageTransformer;
11
use Victoire\Bundle\CoreBundle\Entity\View;
12
use Victoire\Bundle\PageBundle\Entity\PageStatus;
13
use Victoire\Bundle\PageBundle\Helper\PageHelper;
14
use Victoire\Bundle\UserBundle\Model\User;
15
use Victoire\Bundle\ViewReferenceBundle\Connector\ViewReferenceRepository;
16
use Victoire\Bundle\ViewReferenceBundle\Exception\ViewReferenceNotFoundException;
17
18
/**
19
 * Article Manager.
20
 *
21
 * ref. victoire_blog.manager.article.
22
 */
23
class ArticleManager
24
{
25
    private $entityManager;
26
    private $businessPageBuilder;
27
    private $virtualToBusinessPageTransformer;
28
    private $pageHelper;
29
    private $viewReferenceRepo;
30
31
    public function __construct(
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
32
        EntityManager $entityManager,
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
33
        BusinessPageBuilder $businessPageBuilder,
34
        VirtualToBusinessPageTransformer $virtualToBusinessPageTransformer,
35
        PageHelper $pageHelper,
36
        ViewReferenceRepository $viewReferenceRepo
37
    ) {
38
        $this->entityManager = $entityManager;
39
        $this->businessPageBuilder = $businessPageBuilder;
40
        $this->virtualToBusinessPageTransformer = $virtualToBusinessPageTransformer;
41
        $this->pageHelper = $pageHelper;
42
        $this->viewReferenceRepo = $viewReferenceRepo;
43
    }
44
45
    /**
46
     * Create Article with its author, tags.
47
     * Create BusinessPage for this Article.
48
     *
49
     * @param Article $article
50
     * @param User    $author
51
     *
52
     * @return BusinessPage
53
     */
54
    public function create(Article $article, User $author)
55
    {
56
        $article->setAuthor($author);
57
58
        /** @var Tag[] $tags */
59
        $tags = $article->getTags();
60
        if (is_array($tags)) {
61
            foreach ($tags as $tag) {
62
                $tag->setBlog($article->getBlog());
63
                $this->entityManager->persist($tag);
64
            }
65
        }
66
67
        //Article has to be persisted before BusinessPage generation
68
        $this->entityManager->persist($article);
69
        $this->entityManager->flush();
70
71
        $page = $this->businessPageBuilder->generateEntityPageFromTemplate(
72
            $article->getTemplate(),
73
            $article,
74
            $this->entityManager
75
        );
76
77
        //Transform VBP into BP
78
        $this->virtualToBusinessPageTransformer->transform($page);
79
        $page->setParent($article->getBlog());
80
81
        $this->entityManager->persist($page);
82
        $this->entityManager->flush();
83
84
        return $page;
85
    }
86
87
    /**
88
     * Update Blog Article settings.
89
     *
90
     * @param Article $article
91
     *
92
     * @throws ViewReferenceNotFoundException
93
     *
94
     * @return View
95
     */
96
    public function updateSettings(Article $article)
97
    {
98
        //Update Tags
99
        /** @var Tag[] $tags */
100
        $tags = $article->getTags();
101
        if (is_array($tags)) {
102
            foreach ($tags as $tag) {
103
                $tag->setBlog($article->getBlog());
104
                $this->entityManager->persist($tag);
105
            }
106
        }
107
108
        //Update BusinessPage
109
        $businessPage = $this->pageHelper->findPageByParameters([
110
            'viewId'   => $article->getTemplate()->getId(),
111
            'entityId' => $article->getId(),
112
        ]);
113
        $template = $article->getTemplate();
114
        $businessPage->setTemplate($template);
115
116
        //Update Page
117
        $page = $this->pageHelper->findPageByParameters([
118
            'viewId'   => $template->getId(),
119
            'entityId' => $article->getId(),
120
        ]);
121
        $page->setName($article->getName());
122
        $page->setSlug($article->getSlug());
123
        $page->setStatus($article->getStatus());
124
125
        $this->entityManager->flush();
126
127
        //Set ViewReference for Page redirection
128
        $viewReference = $this->viewReferenceRepo->getOneReferenceByParameters(
129
            ['viewId' => $page->getId()]
130
        );
131
        $page->setReference($viewReference);
132
133
        return $page;
134
    }
135
136
    /**
137
     * Delete a given Article.
138
     *
139
     * @param Article $article
140
     */
141
    public function delete(Article $article)
142
    {
143
        $bep = $this->pageHelper->findPageByParameters(
144
            [
145
                'templateId' => $article->getTemplate()->getId(),
146
                'entityId'   => $article->getId(),
147
            ]
148
        );
149
        $this->entityManager->remove($bep);
0 ignored issues
show
Bug introduced by
It seems like $bep defined by $this->pageHelper->findP... => $article->getId())) on line 143 can be null; however, Doctrine\ORM\EntityManager::remove() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
150
151
        $article->setVisibleOnFront(0);
152
        $article->setDeletedAt(new \DateTime());
153
        $article->setStatus(PageStatus::DELETED);
154
155
        $this->entityManager->flush();
156
    }
157
}
158