Completed
Push — master ( f93802...a3beff )
by Paweł
34:14
created

LoadArticlesMediaData   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 109
Duplicated Lines 15.6 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 17
loc 109
ccs 0
cts 81
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 8 1
B loadArticles() 17 80 5
A getOrder() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
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\Model\ImageRendition;
21
use SWP\Bundle\ContentBundle\Model\ArticleInterface;
22
use SWP\Bundle\FixturesBundle\AbstractFixture;
23
use Symfony\Component\HttpFoundation\File\UploadedFile;
24
25
class LoadArticlesMediaData extends AbstractFixture implements FixtureInterface, OrderedFixtureInterface
26
{
27
    private $manager;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(ObjectManager $manager)
33
    {
34
        $this->manager = $manager;
35
        $env = $this->getEnvironment();
36
        $this->loadArticles($env, $manager);
37
38
        $manager->flush();
39
    }
40
41
    /**
42
     * Sets articles manually (not via Alice) for test env due to fatal error:
43
     * Method PHPCRProxies\__CG__\Doctrine\ODM\PHPCR\Document\Generic::__toString() must not throw an exception.
44
     */
45
    public function loadArticles($env, $manager)
46
    {
47
        $articles = [
48
            'test' => [
49
                [
50
                    'title' => 'Test news article',
51
                    'content' => 'Test news article content',
52
                    'locale' => 'en',
53
                ],
54
            ],
55
        ];
56
57
        $renditions = [
58
            'original' => [
59
                'width' => '4000',
60
                'height' => '2667',
61
                'media' => '12345678987654321a',
62
            ],
63
            '16-9' => [
64
                'width' => '1079',
65
                'height' => '720',
66
                'media' => '12345678987654321b',
67
            ],
68
            '4-3' => [
69
                'width' => '800',
70
                'height' => '533',
71
                'media' => '12345678987654321c',
72
            ],
73
        ];
74
75
        $mediaManager = $this->container->get('swp_content_bundle.manager.media');
76
        $fakeImage = __DIR__.'/../../Resources/assets/test_cc_image.jpg';
77
78
        if (isset($articles[$env])) {
79
            foreach ($articles[$env] as $articleData) {
80
                $article = $this->container->get('swp.factory.article')->create();
81
                $article->setTitle($articleData['title']);
82
                $article->setBody($articleData['content']);
83
                $article->setLocale($articleData['locale']);
84
                $article->setPublishedAt(new \DateTime());
85
                $article->setPublishable(true);
86
                $article->setStatus(ArticleInterface::STATUS_PUBLISHED);
87
                $manager->persist($article);
88
89
                // create Media
90
                $articleMediaClass = $this->container->getParameter('swp.model.media.class');
91
                $articleMedia = new $articleMediaClass();
92
                $articleMedia->setArticle($article);
93
                $articleMedia->setKey('embedded6358005131');
94
                $articleMedia->setBody('article media body');
95
                $articleMedia->setByLine('By Best Editor');
96
                $articleMedia->setLocated('Porto');
97
                $articleMedia->setDescription('Media description');
98
                $articleMedia->setUsageTerms('Some super open terms');
99
                $articleMedia->setMimetype('image/jpeg');
100
                $manager->persist($articleMedia);
101
102
                /* @var $rendition Rendition */
103 View Code Duplication
                foreach ($renditions as $key => $rendition) {
104
                    $uploadedFile = new UploadedFile($fakeImage, $rendition['media'], 'image/jpeg', filesize($fakeImage), null, true);
105
                    $image = $mediaManager->handleUploadedFile($uploadedFile, $rendition['media']);
106
107
                    if ($key === 'original') {
108
                        $articleMedia->setImage($image);
109
                    }
110
111
                    $imageRendition = new ImageRendition();
112
                    $imageRendition->setImage($image);
113
                    $imageRendition->setHeight($rendition['height']);
114
                    $imageRendition->setWidth($rendition['width']);
115
                    $imageRendition->setName($key);
116
                    $imageRendition->setMedia($articleMedia);
117
                    $articleMedia->addRendition($imageRendition);
118
                    $manager->persist($imageRendition);
119
                }
120
            }
121
122
            $manager->flush();
123
        }
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getOrder()
130
    {
131
        return 999;
132
    }
133
}
134