Issues (879)

src/App/Fixtures/AbstractFixture.php (7 issues)

1
<?php
2
/**
3
 * AbstractFixture.php
4
 *
5
 * @since 27/07/16
6
 * @author gseidel
7
 */
8
9
namespace App\Fixtures;
10
11
use Doctrine\Common\DataFixtures\FixtureInterface;
0 ignored issues
show
The type Doctrine\Common\DataFixtures\FixtureInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
0 ignored issues
show
The type Doctrine\Common\DataFixt...OrderedFixtureInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Doctrine\Common\Persistence\ObjectManager;
0 ignored issues
show
The type Doctrine\Common\Persistence\ObjectManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Enhavo\Bundle\BlockBundle\Entity\Node;
15
use Enhavo\Bundle\BlockBundle\Factory\BlockFactory;
16
use Enhavo\Bundle\BlockBundle\Factory\NodeFactory;
17
use Enhavo\Bundle\BlockBundle\Model\Block\PictureBlock;
18
use Enhavo\Bundle\BlockBundle\Model\Block\TextBlock;
19
use Enhavo\Bundle\BlockBundle\Model\Block\TextPictureBlock;
20
use Enhavo\Bundle\BlockBundle\Model\BlockInterface;
21
use Enhavo\Bundle\BlockBundle\Model\NodeInterface;
22
use Enhavo\Bundle\RoutingBundle\Entity\Route;
23
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
use Symfony\Component\PropertyAccess\PropertyAccess;
26
use Symfony\Component\Yaml\Yaml;
27
28
abstract class AbstractFixture implements FixtureInterface, OrderedFixtureInterface, ContainerAwareInterface
29
{
30
    /**
31
     * @var ObjectManager
32
     */
33
    protected $manager;
34
35
    /**
36
     * @var ContainerInterface
37
     */
38
    protected $container;
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function load(ObjectManager $manager)
44
    {
45
        $this->manager = $manager;
46
        $this->loadData($this->getName());
47
    }
48
49
    /**
50
     * Load the data from fixture file and insert it into the database
51
     *
52
     * @param $name
53
     * @throws \Exception
54
     */
55
    public function loadData($name) {
56
57
        $file = sprintf('%s/../Resources/fixtures/%s.yaml', __DIR__ , $name);
58
59
        if(!file_exists($file)) {
60
            throw new \Exception(sprintf('fixtures file "%s" not found for name "%s"', $file, $name));
61
        }
62
63
        $data = Yaml::parseFile($file);
64
65
        $items = [];
66
        foreach ($data as $args) {
67
            $item = $this->create($args);
68
            $this->manager->persist($item);
69
            $items[] = $item;
70
        }
71
72
        $this->manager->flush();
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function setContainer(ContainerInterface $container = null)
79
    {
80
        $this->container = $container;
81
    }
82
83
    /**
84
     * Save file and return its model.
85
     *
86
     * @param $path
87
     * @return \Enhavo\Bundle\MediaBundle\Model\FileInterface
88
     * @throws \Exception
89
     */
90
    protected function createImage($path)
91
    {
92
        $path = sprintf('%s/../Resources/images/%s', __DIR__, $path);
93
        $file = $this->container->get('enhavo_media.factory.file')->createFromPath($path);
94
        $this->container->get('enhavo_media.media.media_manager')->saveFile($file);
95
96
        return $file;
97
    }
98
99
    /**
100
     * Return DateTime object
101
     *
102
     * @param $value
103
     * @return \DateTime
104
     */
105
    public function createDateTime($value)
106
    {
107
        $date = null;
108
109
        if(is_string($value)) {
110
            $date = new \DateTime($value);
111
        }
112
113
        if(is_int($value)) {
114
            $date = new \DateTime();
115
            $date->setTimestamp($value);
116
        }
117
118
        return $date;
119
    }
120
121
    /**
122
     * Return single Entity by Argument
123
     *
124
     * @param $args
125
     * @return mixed
126
     */
127
    abstract function create($args);
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
129
    /**
130
     * Return the name of the fixture
131
     *
132
     * @return mixed
133
     */
134
    abstract function getName();
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
136
    /**
137
     * {@inheritDoc}
138
     */
139
    abstract function getOrder();
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
141
    /**
142
     * @param $container
143
     * @return NodeInterface
144
     */
145
    protected function createContent($container)
146
    {
147
        /** @var NodeFactory $nodeFactory */
148
        $nodeFactory = $this->container->get('enhavo_block.factory.node');
149
        $containerEntity = $nodeFactory->createNew();
150
151
        $position = 0;
152
        foreach($container as $fields) {
153
            $node = $nodeFactory->createNew();
154
            $type = $fields['type'];
155
            unset($fields['type']);
156
157
            $content = false;
158
            $setter = 'content';
159
            if (isset($fields['content'])) {
160
                $content = $fields['content'];
161
                unset($fields['content']);
162
            }
163
            if (isset($fields['setter'])) {
164
                $setter = $fields['setter'];
165
                unset($fields['setter']);
166
            }
167
            $block = $this->createBlockType($type, $fields);
168
169
            if ($content !== false) {
170
                $subNode = $this->createContent($content);
171
                if ($setter !== false) {
172
                    $accessor = PropertyAccess::createPropertyAccessor();
173
                    $accessor->setValue($block, $setter, $subNode);
174
                }
175
            }
176
177
            $node->setName($type);
178
            $node->setBlock($block);
179
            $node->setPosition($position++);
180
            $containerEntity->addChild($node);
181
        }
182
        $this->translate($containerEntity);
183
        return $containerEntity;
184
    }
185
186
    protected function createNodes(NodeInterface $parent, array $container)
187
    {
188
        /** @var NodeFactory $nodeFactory */
189
        $nodeFactory = $this->container->get('enhavo_block.factory.node');
190
        $nodes = [];
0 ignored issues
show
The assignment to $nodes is dead and can be removed.
Loading history...
191
        $position = 0;
192
        foreach($container as $fields) {
193
            $node = $nodeFactory->createNew();
194
            $type = $fields['type'];
195
            unset($fields['type']);
196
197
            $content = false;
198
            if (isset($fields['content'])) {
199
                $content = $fields['content'];
200
                unset($fields['content']);
201
            }
202
            $block = $this->createBlockType($type, $fields);
203
204
            $node->setName($type);
205
            $node->setBlock($block);
206
            $node->setPosition($position++);
207
208
            if ($content !== false) {
209
                $this->createNodes($node, $content);
210
            }
211
212
            $parent->addChild($node);
213
        }
214
    }
215
216
    /**
217
     * @param $type
218
     * @param $fields
219
     * @return BlockInterface
220
     */
221
    protected function createBlockType($type, $fields)
222
    {
223
        /** @var BlockFactory $factory */
224
        $factory = $this->container->get('enhavo_block.factory.block');
225
        $itemType = $factory->createNew($type);
226
227
        $this->setFields($type, $itemType, $fields);
228
        $this->translate($itemType);
229
        return $itemType;
230
    }
231
232
    protected function setFields($type, $blockType, $fields)
233
    {
234
        switch($type) {
235
            case('text'):
236
                /** @var $blockType TextBlock */
237
                $blockType->setText($fields['text']);
238
                $blockType->setTitle($fields['title']);
239
                break;
240
            case('picture'):
241
                /** @var $blockType PictureBlock */
242
                $blockType->setFile($this->createImage($fields['file']));
243
                $blockType->setTitle($fields['title']);
244
                $blockType->setCaption($fields['caption']);
245
                break;
246
            case('text_picture'):
247
                /** @var $blockType TextPictureBlock */
248
                $blockType->setFile($this->createImage($fields['file']));
249
                $blockType->setTitle($fields['title']);
250
                $blockType->setCaption($fields['caption']);
251
                $blockType->setFloat($fields['float']);
252
                $blockType->setTitle($fields['title']);
253
                $blockType->setText($fields['text']);
254
                break;
255
            case('sidebar_column'):
256
                $code = $fields['code'];
257
                $sidebar = $this->manager->getRepository('EnhavoSidebarBundle:Sidebar')->findOneBy([
258
                    'code' => $code
259
                ]);
260
                $blockType->setSidebar($sidebar);
261
                break;
262
        }
263
    }
264
265
    protected function createRoute($url, $content)
266
    {
267
        $route = $this->container->get('enhavo_demo.factory.route')->createNew();
268
        $route->setName(preg_replace('/ */', '', strtolower($content->getTitle())));
269
        $route->setStaticPrefix($url);
270
        $route->setContent($content);
271
        $this->translate($route);
272
        return $route;
273
    }
274
275
    protected function translate($entity)
276
    {
277
        /** @var Translator $translator */
278
//        $translator = $this->container->get('enhavo_translation.translator');
279
//        $metadata = $translator->getMetadata($entity);
280
//
281
//        if($metadata === null) {
282
//            return;
283
//        }
284
//
285
//        $accessor = PropertyAccess::createPropertyAccessor();
286
//        foreach($metadata->getProperties() as $property) {
287
//            $formData = $accessor->getValue($entity, $property->getName());
288
//            $translationData = $translator->normalizeToTranslationData($entity, $property->getName(), $formData);
289
//            $translator->addTranslationData($entity, $property->getName(), $translationData);
290
//            $normalizeData = $translator->normalizeToModelData($entity, $property->getName(), $formData);
291
//            $accessor->setValue($entity, $property->getName(), $normalizeData);
292
//        }
293
    }
294
}
295