Completed
Push — master ( a682ba...4851b3 )
by Paweł
40:50
created

LoadCollectionRouteArticles::setRoutesContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 2
crap 3
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\ContentBundle\Doctrine\ORM\Article;
21
use SWP\Bundle\ContentBundle\Doctrine\ORM\Route;
22
use SWP\Bundle\FixturesBundle\AbstractFixture;
23
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
24
use SWP\Component\Common\Criteria\Criteria;
25
26
class LoadCollectionRouteArticles extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
27
{
28
    private $manager;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 7 View Code Duplication
    public function load(ObjectManager $manager)
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...
34
    {
35 7
        $this->manager = $manager;
36 7
        $env = $this->getEnvironment();
37
38 7
        if ('test' !== $env) {
39
            return;
40
        }
41
42 7
        $this->loadRoutes($env, $manager);
43 7
        $this->loadArticles($env, $manager);
44 7
        $this->setRoutesContent($env, $manager);
45
46 7
        $manager->flush();
47 7
    }
48
49 7
    public function loadRoutes($env, ObjectManager $manager)
50
    {
51
        $routes = [
52
            'test' => [
53
                [
54
                    'name' => 'collection-no-template',
55
                    'type' => 'collection',
56
                ],
57
                [
58
                    'name' => 'collection-test',
59
                    'type' => 'collection',
60
                    'template_name' => 'collection.html.twig',
61
                ],
62
                [
63
                    'name' => 'collection-content',
64
                    'type' => 'collection',
65
                    'articles_template_name' => 'test.html.twig',
66
                ],
67
                [
68
                    'name' => 'collection-with-content',
69
                    'type' => 'collection',
70
                    'articles_template_name' => 'test.html.twig',
71
                ],
72 7
            ],
73
        ];
74
75 7
        $routeService = $this->container->get('swp.service.route');
76
77 7
        foreach ($routes[$env] as $routeData) {
78 7
            $route = new Route();
79 7
            $route->setName($routeData['name']);
80 7
            $route->setType($routeData['type']);
81
82 7
            if (isset($routeData['template_name'])) {
83 7
                $route->setTemplateName($routeData['template_name']);
84
            }
85
86 7
            if (isset($routeData['articles_template_name'])) {
87 7
                $route->setArticlesTemplateName($routeData['articles_template_name']);
88
            }
89
90 7
            $routeService->createRoute($route);
91
92 7
            $manager->persist($route);
93
        }
94
95 7
        $manager->flush();
96 7
    }
97
98 7
    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...
99
    {
100
        $routes = [
101
            'test' => [
102
                [
103
                    'name' => 'collection-with-content',
104
                    'content' => 'content-assigned-as-route-content',
105
                ],
106 7
            ],
107
        ];
108
109 7
        foreach ($routes[$env] as $routeData) {
110 7
            if (array_key_exists('content', $routeData)) {
111 7
                $articleProvider = $this->container->get('swp.provider.article');
112 7
                $routeProvider = $this->container->get('swp.provider.route');
113 7
                $route = $routeProvider->getRouteByName($routeData['name']);
114 7
                $criteria = new Criteria();
115 7
                $criteria->set('slug', $routeData['content']);
116 7
                $route->setContent($articleProvider->getOneByCriteria($criteria));
117
            }
118
        }
119 7
    }
120
121 7
    public function loadArticles($env, $manager)
122
    {
123
        $articles = [
124
            'test' => [
125
                [
126
                    'title' => 'Test art1',
127
                    'content' => 'Test art1 content',
128
                    'route' => 'collection-test',
129
                    'locale' => 'en',
130
                ],
131
                [
132
                    'title' => 'Test art2',
133
                    'content' => 'Test art2 content',
134
                    'route' => 'collection-test',
135
                    'locale' => 'en',
136
                ],
137
                [
138
                    'title' => 'Test art3',
139
                    'content' => 'Test art3',
140
                    'route' => 'collection-test',
141
                    'locale' => 'en',
142
                ],
143
                [
144
                    'title' => 'Some content',
145
                    'content' => 'some content',
146
                    'route' => 'collection-content',
147
                    'locale' => 'en',
148
                    'templateName' => 'some_content.html.twig',
149
                ],
150
                [
151
                    'title' => 'Some other content',
152
                    'content' => 'some other content',
153
                    'route' => 'collection-content',
154
                    'locale' => 'en',
155
                ],
156
                [
157
                    'title' => 'Content assigned as route content',
158
                    'content' => 'some other content assigned as route content',
159
                    'locale' => 'en',
160
                ],
161 7
            ],
162
        ];
163
164 7
        if (isset($articles[$env])) {
165 7
            $routeProvider = $this->container->get('swp.provider.route');
166 7
            foreach ($articles[$env] as $articleData) {
167 7
                $article = new Article();
168 7
                $article->setTitle($articleData['title']);
169 7
                $article->setBody($articleData['content']);
170 7
                $article->setLocale($articleData['locale']);
171 7
                $article->setPublishedAt(new \DateTime());
172 7
                $article->setPublishable(true);
173 7
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
174 7
                if (isset($articleData['templateName'])) {
175 7
                    $article->setTemplateName($articleData['templateName']);
176
                }
177 7
                if (isset($articleData['route'])) {
178 7
                    $article->setRoute($routeProvider->getRouteByName($articleData['route']));
179
                }
180
181 7
                $manager->persist($article);
182
            }
183
184 7
            $manager->flush();
185
        }
186 7
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191 7
    public function getOrder()
192
    {
193 7
        return 20;
194
    }
195
}
196