Completed
Push — master ( 530ce9...1b2ae5 )
by Paweł
23:42 queued 20:31
created

LoadArticlesData::loadRoutes()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 123
Code Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 0
Metric Value
c 6
b 2
f 0
dl 0
loc 123
rs 6.4589
cc 7
eloc 71
nc 9
nop 2

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.u. 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
namespace SWP\Bundle\FixturesBundle\DataFixtures\PHPCR;
15
16
use Doctrine\Common\DataFixtures\FixtureInterface;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use SWP\Bundle\ContentBundle\Document\Article;
19
use SWP\Bundle\FixturesBundle\AbstractFixture;
20
use SWP\Bundle\ContentBundle\Document\Route;
21
22
class LoadArticlesData extends AbstractFixture implements FixtureInterface
23
{
24
    private $manager;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function load(ObjectManager $manager)
30
    {
31
        $this->manager = $manager;
32
        $env = $this->getEnvironment();
33
34
        $this->loadRoutes($env, $manager);
35
        $this->loadArticles($env, $manager);
36
        $this->setRoutesContent($env, $manager);
37
38
        $manager->flush();
39
    }
40
41
    public function loadRoutes($env, $manager)
42
    {
43
        $routes = [
44
            'dev' => [
45
                [
46
                    'parent' => '/swp/default/routes',
47
                    'name' => 'news',
48
                    'variablePattern' => '/{slug}',
49
                    'requirements' => [
50
                        'slug' => '[a-zA-Z1-9\-_\/]+',
51
                    ],
52
                    'defaults' => [
53
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContainerPageAction',
54
                    ],
55
                ],
56
                [
57
                    'parent' => '/swp/default/routes',
58
                    'name' => 'articles',
59
                    'defaults' => [
60
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
61
                    ],
62
                ],
63
                [
64
                    'parent' => '/swp/default/routes/articles',
65
                    'name' => 'get-involved',
66
                    'defaults' => [
67
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
68
                    ],
69
                ],
70
                [
71
                    'parent' => '/swp/default/routes/articles',
72
                    'name' => 'features',
73
                    'defaults' => [
74
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
75
                    ],
76
                ],
77
            ],
78
            'test' => [
79
                [
80
                    'parent' => '/swp/default/routes',
81
                    'name' => 'news',
82
                    'variablePattern' => '/{slug}',
83
                    'requirements' => [
84
                        'slug' => '[a-zA-Z1-9\-_\/]+',
85
                    ],
86
                    'defaults' => [
87
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContainerPageAction',
88
                    ],
89
                ],
90
                [
91
                    'parent' => '/swp/client1/routes',
92
                    'name' => 'news',
93
                    'variablePattern' => '/{slug}',
94
                    'requirements' => [
95
                        'slug' => '[a-zA-Z1-9\-_\/]+',
96
                    ],
97
                    'defaults' => [
98
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContainerPageAction',
99
                    ],
100
                ],
101
                [
102
                    'parent' => '/swp/default/routes',
103
                    'name' => 'articles',
104
                    'defaults' => [
105
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
106
                    ],
107
                ],
108
                [
109
                    'parent' => '/swp/default/routes/articles',
110
                    'name' => 'features',
111
                    'defaults' => [
112
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
113
                    ],
114
                ],
115
                [
116
                    'parent' => '/swp/client1/routes',
117
                    'name' => 'features',
118
                    'defaults' => [
119
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContentPageAction',
120
                    ],
121
                ],
122
                [
123
                    'parent' => '/swp/default/routes',
124
                    'name' => 'homepage',
125
                    'defaults' => [
126
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContainerPageAction',
127
                    ],
128
                ],
129
                [
130
                    'parent' => '/swp/client1/routes',
131
                    'name' => 'homepage',
132
                    'defaults' => [
133
                        '_controller' => '\SWP\Bundle\WebRendererBundle\Controller\ContentController::renderContainerPageAction',
134
                    ],
135
                ],
136
            ],
137
        ];
138
139
        foreach ($routes[$env] as $routeData) {
140
            $route = new Route();
141
            $route->setParentDocument($manager->find(null, $routeData['parent']));
142
            $route->setName($routeData['name']);
143
144
            if (isset($routeData['variablePattern'])) {
145
                $route->setVariablePattern($routeData['variablePattern']);
146
            }
147
148
            if (isset($routeData['requirements'])) {
149
                foreach ($routeData['requirements'] as $key => $value) {
150
                    $route->setRequirement($key, $value);
151
                }
152
            }
153
154
            if (isset($routeData['defaults'])) {
155
                foreach ($routeData['defaults'] as $key => $value) {
156
                    $route->setDefault($key, $value);
157
                }
158
            }
159
            $manager->persist($route);
160
        }
161
162
        $manager->flush();
163
    }
164
165
    public function setRoutesContent($env, $manager)
166
    {
167
        $routes = [
168
            'dev' => [
169
                [
170
                    'path' => '/swp/default/routes/news',
171
                    'content' => '/swp/default/content/features',
172
                ],
173
                [
174
                    'path' => '/swp/default/routes/articles/features',
175
                    'content' => '/swp/default/content/features',
176
                ],
177
                [
178
                    'path' => '/swp/default/routes/articles/get-involved',
179
                    'content' => '/swp/default/content/get-involved',
180
                ],
181
            ],
182
            'test' => [
183
                [
184
                    'path' => '/swp/default/routes/news',
185
                    'content' => '/swp/default/content/test-news-article',
186
                ],
187
                [
188
                    'path' => '/swp/default/routes/articles/features',
189
                    'content' => '/swp/default/content/features',
190
                ],
191
                [
192
                    'path' => '/swp/client1/routes/features',
193
                    'content' => '/swp/client1/content/features-client1',
194
                ],
195
            ],
196
        ];
197
198
        foreach ($routes[$env] as $routeData) {
199
            if (array_key_exists('content', $routeData)) {
200
                $route = $manager->find(null, $routeData['path']);
201
                $route->setContent($manager->find(null, $routeData['content']));
202
            }
203
        }
204
    }
205
206
    /**
207
     * Sets articles manually (not via Alice) for test env due to fatal error:
208
     * Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception.
209
     */
210
    public function loadArticles($env, $manager)
211
    {
212
        if ($env !== 'test') {
213
            $this->loadFixtures(
214
                '@SWPFixturesBundle/Resources/fixtures/PHPCR/'.$env.'/article.yml',
215
                $manager,
216
                [
217
                    'providers' => [$this],
218
                ]
219
            );
220
        }
221
222
        $articles = [
223
            'test' => [
224
                [
225
                    'title' => 'Test news article',
226
                    'content' => 'Test news article content',
227
                    'route' => '/swp/default/routes/news',
228
                    'parent' => '/swp/default/content',
229
                ],
230
                [
231
                    'title' => 'Test article',
232
                    'content' => 'Test article content',
233
                    'route' => '/swp/default/routes/news',
234
                    'parent' => '/swp/default/content',
235
                ],
236
                [
237
                    'title' => 'Features',
238
                    'content' => 'Features content',
239
                    'route' => '/swp/default/routes/news',
240
                    'parent' => '/swp/default/content',
241
                ],
242
                [
243
                    'title' => 'Features client1',
244
                    'content' => 'Features client1 content',
245
                    'route' => '/swp/client1/routes/news',
246
                    'parent' => '/swp/client1/content',
247
                ],
248
            ],
249
        ];
250
251
        if (isset($articles[$env])) {
252
            foreach ($articles[$env] as $articleData) {
253
                $article = new Article();
254
                $article->setParent($manager->find(null, $articleData['parent']));
255
                $article->setTitle($articleData['title']);
256
                $article->setContent($articleData['content']);
257
                $article->setRoute($manager->find(null, $articleData['route']));
258
259
                $manager->persist($article);
260
            }
261
262
            $manager->flush();
263
        }
264
    }
265
}
266