Completed
Push — master ( ae5e03...0447ee )
by Jeroen
10:35 queued 04:37
created

NodeBundle/Helper/Services/PageCreatorService.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\NodeBundle\Helper\Services;
4
5
use Doctrine\ORM\EntityManager;
6
use Doctrine\ORM\ORMException;
7
use Kunstmaan\AdminBundle\Repository\UserRepository;
8
use Kunstmaan\NodeBundle\Entity\HasNodeInterface;
9
use Kunstmaan\NodeBundle\Entity\Node;
10
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
11
use Kunstmaan\NodeBundle\Repository\NodeRepository;
12
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
13
use Kunstmaan\SeoBundle\Entity\Seo;
14
use Kunstmaan\SeoBundle\Repository\SeoRepository;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
17
/**
18
 * Service to create new pages.
19
 */
20
class PageCreatorService
21
{
22
    /**
23
     * @var EntityManager
24
     */
25
    protected $entityManager;
26
27
    /**
28
     * @var ACLPermissionCreatorService
29
     */
30
    protected $aclPermissionCreatorService;
31
32
    /**
33
     * @var string
34
     */
35
    protected $userEntityClass;
36
37
    public function setEntityManager($entityManager)
38
    {
39
        $this->entityManager = $entityManager;
40
    }
41
42
    public function setACLPermissionCreatorService($aclPermissionCreatorService)
43
    {
44
        $this->aclPermissionCreatorService = $aclPermissionCreatorService;
45
    }
46
47
    public function setUserEntityClass($userEntityClass)
48
    {
49
        $this->userEntityClass = $userEntityClass;
50
    }
51
52
    /**
53
     * Sets the Container. This is still here for backwards compatibility.
54
     *
55
     * The ContainerAwareInterface has been removed so the container won't be injected automatically.
56
     * This function is just there for code that calls it manually.
57
     *
58
     * @param ContainerInterface $container a ContainerInterface instance
0 ignored issues
show
Should the type for parameter $container not be null|ContainerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
59
     *
60
     * @api
61
     */
62
    public function setContainer(ContainerInterface $container = null)
63
    {
64
        $this->setEntityManager($container->get('doctrine.orm.entity_manager'));
0 ignored issues
show
It seems like $container is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
65
        $this->setACLPermissionCreatorService($container->get('kunstmaan_node.acl_permission_creator_service'));
66
        $this->setUserEntityClass($container->getParameter('fos_user.model.user.class'));
67
    }
68
69
    /**
70
     * @param HasNodeInterface $pageTypeInstance the page
71
     * @param array            $translations     Containing arrays. Sample:
72
     *                                           [
73
     *                                           [   "language" => "nl",
74
     *                                           "callback" => function($page, $translation) {
75
     *                                           $translation->setTitle('NL titel');
76
     *                                           }
77
     *                                           ],
78
     *                                           [   "language" => "fr",
79
     *                                           "callback" => function($page, $translation) {
80
     *                                           $translation->setTitle('FR titel');
81
     *                                           }
82
     *                                           ]
83
     *                                           ]
84
     *                                           Perhaps it's cleaner when you create one array and append another array for each language.
85
     * @param array            $options          Possible options:
86
     *                                           parent: type node, nodetransation or page.
87
     *                                           page_internal_name: string. name the page will have in the database.
88
     *                                           set_online: bool. if true the page will be set as online after creation.
89
     *                                           hidden_from_nav: bool. if true the page will not be show in the navigation
90
     *                                           creator: username
91
     *
92
     * Automatically calls the ACL + sets the slugs to empty when the page is an Abstract node.
93
     *
94
     * @return Node the new node for the page
95
     *
96
     * @throws \InvalidArgumentException
97
     */
98
    public function createPage(HasNodeInterface $pageTypeInstance, array $translations, array $options = array())
99
    {
100
        if (\is_null($options)) {
101
            $options = array();
102
        }
103
104
        if (\is_null($translations) || (\count($translations) == 0)) {
105
            throw new \InvalidArgumentException('There has to be at least 1 translation in the translations array');
106
        }
107
108
        $em = $this->entityManager;
109
110
        /** @var NodeRepository $nodeRepo */
111
        $nodeRepo = $em->getRepository(Node::class);
112
        /** @var UserRepository $userRepo */
113
        $userRepo = $em->getRepository($this->userEntityClass);
114
        /* @var SeoRepository $seoRepo */
115
        try {
116
            $seoRepo = $em->getRepository(Seo::class);
117
        } catch (ORMException $e) {
118
            $seoRepo = null;
119
        }
120
121
        $pagecreator = \array_key_exists('creator', $options) ? $options['creator'] : 'pagecreator';
122
123
        if ($pagecreator instanceof $this->userEntityClass) {
124
            $creator = $pagecreator;
125
        } else {
126
            $creator = $userRepo->findOneBy(array('username' => $pagecreator));
127
        }
128
129
        $parent = isset($options['parent']) ? $options['parent'] : null;
130
131
        $pageInternalName = isset($options['page_internal_name']) ? $options['page_internal_name'] : null;
132
133
        $setOnline = isset($options['set_online']) ? $options['set_online'] : false;
134
135
        // We need to get the language of the first translation so we can create the rootnode.
136
        // This will also create a translationnode for that language attached to the rootnode.
137
        $first = true;
138
        $rootNode = null;
139
140
        /* @var \Kunstmaan\NodeBundle\Repository\NodeTranslationRepository $nodeTranslationRepo*/
141
        $nodeTranslationRepo = $em->getRepository(NodeTranslation::class);
142
143
        foreach ($translations as $translation) {
144
            $language = $translation['language'];
145
            $callback = $translation['callback'];
146
147
            $translationNode = null;
148
            if ($first) {
149
                $first = false;
150
151
                $em->persist($pageTypeInstance);
152
                $em->flush($pageTypeInstance);
153
154
                // Fetch the translation instead of creating it.
155
                // This returns the rootnode.
156
                $rootNode = $nodeRepo->createNodeFor($pageTypeInstance, $language, $creator, $pageInternalName);
157
158
                if (\array_key_exists('hidden_from_nav', $options)) {
159
                    $rootNode->setHiddenFromNav($options['hidden_from_nav']);
160
                }
161
162
                if (!\is_null($parent)) {
163
                    if ($parent instanceof HasPagePartsInterface) {
164
                        $parent = $nodeRepo->getNodeFor($parent);
0 ignored issues
show
$parent is of type object<Kunstmaan\PagePar...\HasPagePartsInterface>, but the function expects a object<Kunstmaan\NodeBun...ntity\HasNodeInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
                    }
166
                    $rootNode->setParent($parent);
167
                }
168
169
                $em->persist($rootNode);
170
                $em->flush($rootNode);
171
172
                $translationNode = $rootNode->getNodeTranslation($language, true);
173
            } else {
174
                // Clone the $pageTypeInstance.
175
                $pageTypeInstance = clone $pageTypeInstance;
176
177
                $em->persist($pageTypeInstance);
178
                $em->flush($pageTypeInstance);
179
180
                // Create the translationnode.
181
                $translationNode = $nodeTranslationRepo->createNodeTranslationFor($pageTypeInstance, $language, $rootNode, $creator);
182
            }
183
184
            // Make SEO.
185
            $seo = null;
186
187
            if (!\is_null($seoRepo)) {
188
                $seo = $seoRepo->findOrCreateFor($pageTypeInstance);
189
            }
190
191
            $callback($pageTypeInstance, $translationNode, $seo);
192
193
            // Overwrite the page title with the translated title
194
            $pageTypeInstance->setTitle($translationNode->getTitle());
195
            $em->persist($pageTypeInstance);
196
            $em->persist($translationNode);
197
            $em->flush($pageTypeInstance);
198
            $em->flush($translationNode);
199
200
            $translationNode->setOnline($setOnline);
201
202
            if (!\is_null($seo)) {
203
                $em->persist($seo);
204
                $em->flush($seo);
205
            }
206
207
            $em->persist($translationNode);
208
            $em->flush($translationNode);
209
        }
210
211
        // ACL
212
        $this->aclPermissionCreatorService->createPermission($rootNode);
0 ignored issues
show
It seems like $rootNode defined by null on line 138 can be null; however, Kunstmaan\NodeBundle\Hel...ice::createPermission() 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...
213
214
        return $rootNode;
215
    }
216
}
217