Version20131015113854_ChangeImagePaths   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 34
loc 114
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
B up() 34 78 4
A down() 0 5 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
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\DoctrineMigrations;
11
12
use Doctrine\DBAL\Migrations\AbstractMigration;
13
use Doctrine\DBAL\Migrations\SkipMigrationException;
14
use Doctrine\DBAL\Schema\Schema;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\HttpFoundation\File\File;
18
19
/**
20
 * Auto-generated Migration: Please modify to your needs!
21
 */
22
class Version20131015113854_ChangeImagePaths extends AbstractMigration implements ContainerAwareInterface
23
{
24
    /**
25
     * Media dir.
26
     *
27
     * @var string
28
     */
29
    protected $media_dir;
30
31
    /**
32
     * Set container.
33
     *
34
     * @param ContainerInterface $container
35
     */
36
    public function setContainer(ContainerInterface $container = null)
37
    {
38
        $this->media_dir = $container->getParameter('kernel.root_dir').'/../web/media/';
0 ignored issues
show
Bug introduced by
It seems like $container is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
39
    }
40
41
    /**
42
     * @param Schema $schema
43
     *
44
     * @throws SkipMigrationException
45
     */
46
    public function up(Schema $schema)
47
    {
48
        /*
49
         * Migration is not critical
50
         * Old format for storing image files to the new format
51
         *
52
         * Old format(date added image):
53
         *    media/{Y}/{m}/
54
         *
55
         * New Format(date added item):
56
         *    media/{Y}/{m}/{d}/{His}/
57
         */
58
59
        // move covers
60
        $items = $this->connection->fetchAll('
61
            SELECT
62
                `id`,
63
                `cover`,
64
                `date_add`
65
            FROM
66
                `item`
67
            WHERE
68
                `cover` IS NOT NULL AND
69
                `cover` != "" AND
70
                `cover`  NOT LIKE "example/%"'
71
        );
72 View Code Duplication
        foreach ($items as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
73
            $path = date('Y/m/d/His/', strtotime($item['date_add']));
74
            $file = new File($this->media_dir.$item['cover']);
75
            $file->move($this->media_dir.$path);
76
            $this->addSql('
77
                UPDATE
78
                    `item`
79
                SET
80
                    `cover` = ?
81
                WHERE
82
                    `id` = ?',
83
                [
84
                    $path.$file->getBasename(),
85
                    $item['id'],
86
                ]
87
            );
88
        }
89
90
        // move images
91
        $images = $this->connection->fetchAll('
92
            SELECT
93
                im.`id`,
94
                im.`source`,
95
                i.`date_add`
96
            FROM
97
                `item` AS `i`
98
            INNER JOIN
99
                `image` AS `im`
100
                ON
101
                    im.`item` = i.`id`'
102
        );
103 View Code Duplication
        foreach ($images as $image) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
104
            $path = date('Y/m/d/His/', strtotime($image['date_add']));
105
            $file = new File($this->media_dir.$image['source']);
106
            $file->move($this->media_dir.$path);
107
            $this->addSql('
108
                UPDATE
109
                    `image`
110
                SET
111
                    `source` = ?
112
                WHERE
113
                    `id` = ?',
114
                [
115
                    $path.$file->getBasename(),
116
                    $image['id'],
117
                ]
118
            );
119
        }
120
121
        // skip if no data
122
        $this->skipIf(!($items && $images), 'No data to migrate');
0 ignored issues
show
Bug Best Practice introduced by
The expression $items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug Best Practice introduced by
The expression $images of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
123
    }
124
125
    /**
126
     * @param Schema $schema
127
     *
128
     * @throws SkipMigrationException
129
     */
130
    public function down(Schema $schema)
131
    {
132
        // the down migration is not need
133
        $this->skipIf(true, 'The down migration is not need');
134
    }
135
}
136