Completed
Push — master ( bb050e...36e41a )
by Paweł
11:10
created

LoadCollectionRouteArticles::loadArticles()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 71
ccs 19
cts 19
cp 1
rs 8.3216
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 5

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\ORM;
16
17
use Doctrine\Common\DataFixtures\FixtureInterface;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Doctrine\Common\Persistence\ObjectManager;
20
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
21
use SWP\Bundle\CoreBundle\Model\PackageInterface;
22
use SWP\Bundle\FixturesBundle\AbstractFixture;
23
use SWP\Component\Common\Criteria\Criteria;
24
25
class LoadCollectionRouteArticles extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
26
{
27
    private $manager;
28
29
    /**
30
     * {@inheritdoc}
31 7
     */
32
    public function load(ObjectManager $manager)
33 7
    {
34 7
        $this->manager = $manager;
35
        $env = $this->getEnvironment();
36 7
37
        if ('test' !== $env) {
38
            return;
39
        }
40 7
41 7
        $this->loadRoutes($env, $manager);
42 7
        $this->loadArticles($env, $manager);
43
        $this->setRoutesContent($env, $manager);
44 7
45 7
        $manager->flush();
46
    }
47 7
48
    public function loadRoutes($env, ObjectManager $manager)
49
    {
50
        $routes = [
51
            'test' => [
52
                [
53
                    'name' => 'collection-no-template',
54
                    'type' => 'collection',
55
                ],
56
                [
57
                    'name' => 'collection-test',
58
                    'type' => 'collection',
59
                    'template_name' => 'collection.html.twig',
60
                ],
61
                [
62
                    'name' => 'collection-content',
63
                    'type' => 'collection',
64
                    'articles_template_name' => 'test.html.twig',
65
                ],
66
                [
67
                    'name' => 'collection-with-content',
68
                    'type' => 'collection',
69
                    'articles_template_name' => 'test.html.twig',
70 7
                ],
71
            ],
72
        ];
73 7
74
        $routeService = $this->container->get('swp.service.route');
75 7
76 7
        foreach ($routes[$env] as $routeData) {
77 7
            $route = $this->container->get('swp.factory.route')->create();
78 7
            $route->setName($routeData['name']);
79
            $route->setType($routeData['type']);
80 7
81 7
            if (isset($routeData['template_name'])) {
82
                $route->setTemplateName($routeData['template_name']);
83
            }
84 7
85 7
            if (isset($routeData['articles_template_name'])) {
86
                $route->setArticlesTemplateName($routeData['articles_template_name']);
87
            }
88 7
89
            $routeService->createRoute($route);
90 7
91
            $manager->persist($route);
92
        }
93 7
94 7
        $manager->flush();
95
    }
96 7
97
    public function setRoutesContent($env, $manager)
0 ignored issues
show
Unused Code introduced by
The parameter $manager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
    {
99
        $routes = [
100
            'test' => [
101
                [
102
                    'name' => 'collection-with-content',
103
                    'content' => 'content-assigned-as-route-content',
104 7
                ],
105
            ],
106
        ];
107 7
108 7
        foreach ($routes[$env] as $routeData) {
109 7
            if (array_key_exists('content', $routeData)) {
110 7
                $articleProvider = $this->container->get('swp.provider.article');
111 7
                $routeProvider = $this->container->get('swp.provider.route');
112 7
                $route = $routeProvider->getRouteByName($routeData['name']);
113 7
                $criteria = new Criteria();
114 7
                $criteria->set('slug', $routeData['content']);
115
                $route->setContent($articleProvider->getOneByCriteria($criteria));
116
            }
117 7
        }
118
    }
119 7
120
    public function loadArticles($env, $manager)
121
    {
122
        $articles = [
123
            'test' => [
124
                [
125
                    'title' => 'Test art1',
126
                    'content' => 'Test art1 content',
127
                    'route' => 'collection-test',
128
                    'locale' => 'en',
129
                ],
130
                [
131
                    'title' => 'Test art2',
132
                    'content' => 'Test art2 content',
133
                    'route' => 'collection-test',
134
                    'locale' => 'en',
135
                ],
136
                [
137
                    'title' => 'Test art3',
138
                    'content' => 'Test art3',
139
                    'route' => 'collection-test',
140
                    'locale' => 'en',
141
                ],
142
                [
143
                    'title' => 'Some content',
144
                    'content' => 'some content',
145
                    'route' => 'collection-content',
146
                    'locale' => 'en',
147
                    'templateName' => 'some_content.html.twig',
148
                ],
149
                [
150
                    'title' => 'Some other content',
151
                    'content' => 'some other content',
152
                    'route' => 'collection-content',
153
                    'locale' => 'en',
154
                ],
155
                [
156
                    'title' => 'Content assigned as route content',
157
                    'content' => 'some other content assigned as route content',
158
                    'locale' => 'en',
159 7
                ],
160
            ],
161
        ];
162 7
163 7
        if (isset($articles[$env])) {
164 7
            $routeProvider = $this->container->get('swp.provider.route');
165 7
            $articleService = $this->container->get('swp.service.article');
166 7
            foreach ($articles[$env] as $articleData) {
167 7
                /** @var ArticleInterface $article */
168 7
                $article = $this->container->get('swp.factory.article')->create();
169 7
                $article->setTitle($articleData['title']);
170 7
                $article->setBody($articleData['content']);
171 7
                $article->setLocale($articleData['locale']);
172 7
                $article->setCode(md5($articleData['title']));
173 7
                if (isset($articleData['templateName'])) {
174
                    $article->setTemplateName($articleData['templateName']);
175 7
                }
176 7
                if (isset($articleData['route'])) {
177
                    $article->setRoute($routeProvider->getRouteByName($articleData['route']));
178
                }
179 7
180
                $package = $this->createPackage($articleData);
181
                $manager->persist($package);
182 7
                $article->setPackage($package);
183
184 7
                $manager->persist($article);
185
                $articleService->publish($article);
186
            }
187
188
            $manager->flush();
189 7
        }
190
    }
191 7
192 View Code Duplication
    private function createPackage(array $articleData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
193
    {
194
        /** @var PackageInterface $package */
195
        $package = $this->container->get('swp.factory.package')->create();
196
        $package->setHeadline($articleData['title']);
197
        $package->setType('text');
198
        $package->setPubStatus('usable');
199
        $package->setGuid($this->container->get('swp_multi_tenancy.random_string_generator')->generate(10));
200
        $package->setLanguage('en');
201
        $package->setUrgency(1);
202
        $package->setPriority(1);
203
        $package->setVersion(1);
204
205
        return $package;
206
    }
207
208
    /**
209
     * {@inheritdoc}
210
     */
211
    public function getOrder()
212
    {
213
        return 20;
214
    }
215
}
216