Passed
Push — swp-252 ( bc0f1e )
by Rafał
190:48 queued 143:56
created

LoadCollectionRouteArticles::loadRoutes()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 53
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 30
cts 30
cp 1
rs 8.9849
c 0
b 0
f 0
cc 4
eloc 31
nc 5
nop 2
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the Superdesk Web Publisher Fixtures Bundle.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Bundle\FixturesBundle\DataFixtures\PHPCR;
16
17
use Doctrine\Common\DataFixtures\FixtureInterface;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Article;
21
use SWP\Bundle\ContentBundle\Doctrine\ODM\PHPCR\Route;
22
use SWP\Bundle\FixturesBundle\AbstractFixture;
23
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
24
25
class LoadCollectionRouteArticles extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
26
{
27
    private $manager;
28
    private $defaultTenantPrefix;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 7
    public function load(ObjectManager $manager)
34
    {
35 7
        $this->manager = $manager;
36 7
        $env = $this->getEnvironment();
37
38 7
        if ('test' !== $env) {
39
            return;
40
        }
41
42 7
        $this->defaultTenantPrefix = $this->getTenantPrefix();
43
44 7
        $this->loadRoutes($env, $manager);
45 7
        $this->loadArticles($env, $manager);
46 7
        $this->setRoutesContent($env, $manager);
47
48 7
        $manager->flush();
49 7
    }
50
51 7
    public function loadRoutes($env, ObjectManager $manager)
52
    {
53
        $routes = [
54
            'test' => [
55
                [
56 7
                    'parent' => $this->defaultTenantPrefix.'/routes',
57 7
                    'name' => 'collection-no-template',
58 7
                    'type' => 'collection',
59
                ],
60
                [
61 7
                    'parent' => $this->defaultTenantPrefix.'/routes',
62 7
                    'name' => 'collection-test',
63 7
                    'type' => 'collection',
64 7
                    'template_name' => 'collection.html.twig',
65
                ],
66
                [
67 7
                    'parent' => $this->defaultTenantPrefix.'/routes',
68 7
                    'name' => 'collection-content',
69 7
                    'type' => 'collection',
70 7
                    'articles_template_name' => 'test.html.twig',
71
                ],
72
                [
73 7
                    'parent' => $this->defaultTenantPrefix.'/routes',
74 7
                    'name' => 'collection-with-content',
75 7
                    'type' => 'collection',
76 7
                    'articles_template_name' => 'test.html.twig',
77
                ],
78
            ],
79
        ];
80
81 7
        $routeService = $this->container->get('swp.service.route');
82
83 7
        foreach ($routes[$env] as $routeData) {
84 7
            $route = new Route();
85 7
            $route->setParentDocument($manager->find(null, $routeData['parent']));
86 7
            $route->setName($routeData['name']);
87 7
            $route->setType($routeData['type']);
88
89 7
            if (isset($routeData['template_name'])) {
90 7
                $route->setTemplateName($routeData['template_name']);
91
            }
92
93 7
            if (isset($routeData['articles_template_name'])) {
94 7
                $route->setArticlesTemplateName($routeData['articles_template_name']);
95
            }
96
97 7
            $routeService->createRoute($route);
98
99 7
            $manager->persist($route);
100
        }
101
102 7
        $manager->flush();
103 7
    }
104
105 7
    public function setRoutesContent($env, $manager)
106
    {
107
        $routes = [
108
            'test' => [
109
                [
110 7
                    'path' => $this->defaultTenantPrefix.'/routes/collection-with-content',
111 7
                    'content' => $this->defaultTenantPrefix.'/content/content-assigned-as-route-content',
112
                ],
113
            ],
114
        ];
115
116 7 View Code Duplication
        foreach ($routes[$env] as $routeData) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117 7
            if (array_key_exists('content', $routeData)) {
118 7
                $route = $manager->find(null, $routeData['path']);
119 7
                $route->setContent($manager->find(null, $routeData['content']));
120
            }
121
        }
122 7
    }
123
124 7
    public function loadArticles($env, $manager)
125
    {
126
        $articles = [
127
            'test' => [
128
                [
129 7
                    'title' => 'Test art1',
130 7
                    'content' => 'Test art1 content',
131 7
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
132 7
                    'parent' => $this->defaultTenantPrefix.'/content',
133 7
                    'locale' => 'en',
134
                ],
135
                [
136 7
                    'title' => 'Test art2',
137 7
                    'content' => 'Test art2 content',
138 7
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
139 7
                    'parent' => $this->defaultTenantPrefix.'/content',
140 7
                    'locale' => 'en',
141
                ],
142
                [
143 7
                    'title' => 'Test art3',
144 7
                    'content' => 'Test art3',
145 7
                    'route' => $this->defaultTenantPrefix.'/routes/collection-test',
146 7
                    'parent' => $this->defaultTenantPrefix.'/content',
147 7
                    'locale' => 'en',
148
                ],
149
                [
150 7
                    'title' => 'Some content',
151 7
                    'content' => 'some content',
152 7
                    'route' => $this->defaultTenantPrefix.'/routes/collection-content',
153 7
                    'parent' => $this->defaultTenantPrefix.'/content',
154 7
                    'locale' => 'en',
155 7
                    'templateName' => 'some_content.html.twig',
156
                ],
157
                [
158 7
                    'title' => 'Some other content',
159 7
                    'content' => 'some other content',
160 7
                    'route' => $this->defaultTenantPrefix.'/routes/collection-content',
161 7
                    'parent' => $this->defaultTenantPrefix.'/content',
162 7
                    'locale' => 'en',
163
                ],
164
                [
165 7
                    'title' => 'Content assigned as route content',
166 7
                    'content' => 'some other content assigned as route content',
167 7
                    'parent' => $this->defaultTenantPrefix.'/content',
168 7
                    'locale' => 'en',
169
                ],
170
            ],
171
        ];
172
173 7
        if (isset($articles[$env])) {
174 7
            foreach ($articles[$env] as $articleData) {
175 7
                $article = new Article();
176 7
                $article->setParentDocument($manager->find(null, $articleData['parent']));
177 7
                $article->setTitle($articleData['title']);
178 7
                $article->setBody($articleData['content']);
179 7
                $article->setLocale($articleData['locale']);
180 7
                $article->setPublishedAt(new \DateTime());
181 7
                $article->setPublishable(true);
182 7
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
183 7
                if (isset($articleData['templateName'])) {
184 7
                    $article->setTemplateName($articleData['templateName']);
185
                }
186 7
                if (isset($articleData['route'])) {
187 7
                    $article->setRoute($manager->find(null, $articleData['route']));
188
                }
189
190 7
                $manager->persist($article);
191
            }
192
193 7
            $manager->flush();
194
        }
195 7
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 7
    public function getOrder()
201
    {
202 7
        return 20;
203
    }
204
}
205