Completed
Pull Request — master (#314)
by greg
03:20
created

TradingCard::updateModel()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 46
Code Lines 24

Duplication

Lines 10
Ratio 21.74 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 46
rs 8.9411
cc 3
eloc 24
nc 3
nop 2
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 getPath($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 View Code Duplication
    public function getMediaUrl($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...
31
    {
32
        $media_url = $this->getOptions()->getMediaUrl() . '/';
33
        $media_url .= 'game' . $model->getGame()->getId() . '/' . 'model'. $model->getId() . '/';
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
        $path = $this->getPath($model);
45
        $media_url = $this->getMediaUrl($model);
46
        $form  = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_form');
47
        $tradingcard = $this->getGameMapper()->findById($data['trading_card_id']);
48
49
        $model->setGame($tradingcard);
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
        $nb = $game->getBoosterCardNumber();
93
        $booster = [];
94
        $models = $this->getTradingCardModelMapper()->findBy(array('game' => $game));
95
        shuffle($models);
96
97
        $eventModels = $this->getEventManager()->trigger(
98
            __FUNCTION__.'.pre',
99
            $this,
100
            array(
101
            'game' => $game,
102
            'user' => $user,
103
            'entry' => $entry,
104
            'models' => $models
105
            )
106
        )->last();
107
108
        if ($eventModels) {
109
            $models = $eventModels;
110
        }
111
112
        for ($i=1; $i<=$nb; $i++) {
113
            $model = $models[$i];
114
            $card = new \PlaygroundGame\Entity\TradingCardCard();
115
            $card->setUser($user);
116
            $card->setModel($model);
117
            $card->setGame($game);
118
            $card->setEntry($entry);
119
            $card = $this->getTradingCardCardMapper()->insert($card);
120
121
            $booster[] = $card;
122
        }
123
124
        $eventBooster = $this->getEventManager()->trigger(
125
            __FUNCTION__.'.post',
126
            $this,
127
            array(
128
            'game' => $game,
129
            'user' => $user,
130
            'entry' => $entry,
131
            'booster' => $booster
132
            )
133
        )->last();
134
135
        if ($eventBooster) {
136
            $booster = $eventBooster;
137
        }
138
139
        // sending a booster represents an entry. We close the entry after that
140
        $entry->setActive(0);
141
        $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...
142
        
143
        return $booster;
144
    }
145
146
    public function getAlbum($game, $user)
147
    {
148
        // get collection of cards from the user for this game
149
        $album = $this->getTradingCardCardMapper()->findBy(array('game' => $game, 'user' => $user));
150
        $eventAlbum = $this->getEventManager()->trigger(
151
            __FUNCTION__.'.post',
152
            $this,
153
            array(
154
            'game' => $game,
155
            'user' => $user,
156
            'album' => $album
157
            )
158
        )->last();
159
160
        if ($eventAlbum) {
161
            $album = $eventAlbum;
162
        }
163
        
164
        return $album;
165
    }
166
167
    public function getGameEntity()
168
    {
169
        return new \PlaygroundGame\Entity\TradingCard;
170
    }
171
172
    public function getTradingCardMapper()
173
    {
174
        if (null === $this->tradingcardMapper) {
175
            $this->tradingcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcard_mapper');
176
        }
177
178
        return $this->tradingcardMapper;
179
    }
180
181
    public function getTradingCardCardMapper()
182
    {
183
        if (null === $this->tradingcardcardMapper) {
184
            $this->tradingcardcardMapper = $this->getServiceManager()->get('playgroundgame_tradingcardcard_mapper');
185
        }
186
187
        return $this->tradingcardcardMapper;
188
    }
189
190
    public function getTradingCardModelMapper()
191
    {
192
        if (null === $this->tradingcardmodelMapper) {
193
            $this->tradingcardmodelMapper = $this->getServiceManager()->get('playgroundgame_tradingcardmodel_mapper');
194
        }
195
196
        return $this->tradingcardmodelMapper;
197
    }
198
}
199