Completed
Push — master ( 173822...437856 )
by greg
03:29 queued 24s
created

TradingCard   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 211
Duplicated Lines 11.37 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 20
c 5
b 2
f 0
lcom 1
cbo 7
dl 24
loc 211
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getModelPath() 14 14 3
B updateModel() 10 46 3
A getModelMediaUrl() 0 7 1
B getBooster() 0 77 4
A getAlbum() 0 20 2
A getGameEntity() 0 4 1
A getTradingCardMapper() 0 8 2
A getTradingCardCardMapper() 0 8 2
A getTradingCardModelMapper() 0 8 2

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
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManagerAwareInterface;
6
use PlaygroundGame\Mapper\GameInterface as GameMapperInterface;
7
use Zend\Stdlib\ErrorHandler;
8
9
class TradingCard extends Game implements ServiceManagerAwareInterface
10
{
11
    protected $tradingcardMapper;
12
    protected $tradingcardmodelMapper;
13
    protected $tradingcardcardMapper;
14
15 View Code Duplication
    public function getModelPath($model)
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...
16
    {
17
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
18
        $path .= 'game' . $model->getGame()->getId() . DIRECTORY_SEPARATOR;
19
        if (!is_dir($path)) {
20
            mkdir($path, 0777, true);
21
        }
22
        $path .= 'model'. $model->getId() . DIRECTORY_SEPARATOR;
23
        if (!is_dir($path)) {
24
            mkdir($path, 0777, true);
25
        }
26
27
        return $path;
28
    }
29
30
    public function getModelMediaUrl($model)
31
    {
32
        $media_url = $this->getOptions()->getMediaUrl() . '/';
33
        $media_url .= 'game' . $model->getGame()->getId() . '/models/';
34
35
        return $media_url;
36
    }
37
38
    /**
39
     * @param  array $data
40
     * @return \PlaygroundGame\Entity\Game
41
     */
42
    public function updateModel(array $data, $model)
43
    {
44
        $form  = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_form');
45
        $tradingcard = $this->getGameMapper()->findById($data['trading_card_id']);
46
        $model->setGame($tradingcard);
47
        $path = $this->getModelPath($model);
48
        $media_url = $this->getModelMediaUrl($model);
49
50
        $form->bind($model);
51
        $form->setData($data);
52
53
        if (!$form->isValid()) {
54
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by PlaygroundGame\Service\TradingCard::updateModel of type PlaygroundGame\Entity\Game.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
55
        }
56
57 View Code Duplication
        if (!empty($data['upload_image']['tmp_name'])) {
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...
58
            ErrorHandler::start();
59
            $data['upload_image']['name'] = $this->fileNewname(
60
                $path,
61
                $model->getId() . "-" . $data['upload_image']['name']
62
            );
63
            move_uploaded_file($data['upload_image']['tmp_name'], $path . $data['upload_image']['name']);
64
            $model->setImage($media_url . $data['upload_image']['name']);
65
            ErrorHandler::stop(true);
66
        }
67
68
        // if (isset($data['delete_image']) && empty($data['upload_image']['tmp_name'])) {
69
        //     ErrorHandler::start();
70
        //     $image = $model->getImage();
71
        //     $image = str_replace($media_url, '', $image);
72
        //     if (file_exists($path .$image)) {
73
        //         unlink($path .$image);
74
        //     }
75
        //     $model->setImage(null);
76
        //     ErrorHandler::stop(true);
77
        // }
78
79
        $this->getTradingCardModelMapper()->update($model);
80
        $this->getEventManager()->trigger(
81
            __FUNCTION__.'.post',
82
            $this,
83
            array('model' => $model, 'data' => $data)
84
        );
85
86
        return $model;
87
    }
88
89
    public function getBooster($game, $user, $entry)
90
    {
91
        // get booster config from $game
92
        $em = $this->getServiceManager()->get('doctrine.entitymanager.orm_default');
93
        $nb = $game->getBoosterCardNumber();
94
        $booster = [];
95
        
96
        $today = new \DateTime("now");
97
        $today = $today->format('Y-m-d H:i:s');
98
99
        $qb = $em->createQueryBuilder();
100
        $and = $qb->expr()->andx();
101
        $and->add(
102
            $qb->expr()->orX(
103
                $qb->expr()->lte('g.availability', ':date'),
104
                $qb->expr()->isNull('g.availability')
105
            )
106
        );
107
108
        $qb->setParameter('date', $today);
109
        $qb->select('g')
110
        ->from('PlaygroundGame\Entity\TradingCardModel', 'g')
111
        ->where($and);
112
        
113
        $query = $qb->getQuery();
114
        $models = $query->getResult();
115
116
        shuffle($models);
117
118
        $eventModels = $this->getEventManager()->trigger(
119
            __FUNCTION__.'.pre',
120
            $this,
121
            array(
122
            'game' => $game,
123
            'user' => $user,
124
            'entry' => $entry,
125
            'models' => $models
126
            )
127
        )->last();
128
129
        if ($eventModels) {
130
            $models = $eventModels;
131
        }
132
133
        for ($i=0; $i<$nb; $i++) {
134
            $model = $models[$i];
135
            $card = new \PlaygroundGame\Entity\TradingCardCard();
136
            $card->setUser($user);
137
            $card->setModel($model);
138
            $card->setGame($game);
139
            $card->setEntry($entry);
140
            $card = $this->getTradingCardCardMapper()->insert($card);
141
142
            $booster[] = $card;
143
        }
144
145
        $eventBooster = $this->getEventManager()->trigger(
146
            __FUNCTION__.'.post',
147
            $this,
148
            array(
149
            'game' => $game,
150
            'user' => $user,
151
            'entry' => $entry,
152
            'booster' => $booster
153
            )
154
        )->last();
155
156
        if ($eventBooster) {
157
            $booster = $eventBooster;
158
        }
159
160
        // sending a booster represents an entry. We close the entry after that
161
        $entry->setActive(0);
162
        $entry = $this->getEntryMapper()->update($entry);
0 ignored issues
show
Unused Code introduced by
$entry is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
163
        
164
        return $booster;
165
    }
166
167
    public function getAlbum($game, $user)
168
    {
169
        // get collection of cards from the user for this game
170
        $album = $this->getTradingCardCardMapper()->findBy(array('game' => $game, 'user' => $user));
171
        $eventAlbum = $this->getEventManager()->trigger(
172
            __FUNCTION__.'.post',
173
            $this,
174
            array(
175
            'game' => $game,
176
            'user' => $user,
177
            'album' => $album
178
            )
179
        )->last();
180
181
        if ($eventAlbum) {
182
            $album = $eventAlbum;
183
        }
184
        
185
        return $album;
186
    }
187
188
    public function getGameEntity()
189
    {
190
        return new \PlaygroundGame\Entity\TradingCard;
191
    }
192
193
    public function getTradingCardMapper()
194
    {
195
        if (null === $this->tradingcardMapper) {
196
            $this->tradingcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcard_mapper');
197
        }
198
199
        return $this->tradingcardMapper;
200
    }
201
202
    public function getTradingCardCardMapper()
203
    {
204
        if (null === $this->tradingcardcardMapper) {
205
            $this->tradingcardcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcardcard_mapper');
206
        }
207
208
        return $this->tradingcardcardMapper;
209
    }
210
211
    public function getTradingCardModelMapper()
212
    {
213
        if (null === $this->tradingcardmodelMapper) {
214
            $this->tradingcardmodelMapper = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_mapper');
215
        }
216
217
        return $this->tradingcardmodelMapper;
218
    }
219
}
220