Completed
Push — master ( 3460d4...501fc3 )
by Paweł
76:02 queued 29:07
created

LoadCollectionRouteArticles::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004

Importance

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