Completed
Push — master ( 58523e...56a418 )
by greg
37:25 queued 34:17
created

PrizeCategory::getEventManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use Zend\ServiceManager\ServiceManager;
6
use Zend\EventManager\EventManagerAwareTrait;
7
use PlaygroundGame\Options\ModuleOptions;
8
use PlaygroundGame\Mapper\PrizeCategory as PrizeCategoryMapper;
9
use Zend\Stdlib\ErrorHandler;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
use Zend\EventManager\EventManager;
12
13
class PrizeCategory
14
{
15
    use EventManagerAwareTrait;
16
17
    /**
18
     * @var prizeCategoryMapper
19
     */
20
    protected $prizeCategoryMapper;
21
22
    /**
23
     * @var ServiceManager
24
     */
25
    protected $serviceManager;
26
27
    /**
28
     * @var UserServiceOptionsInterface
29
     */
30
    protected $options;
31
32
    /**
33
     *
34
     * @var ServiceManager
35
     */
36
    protected $serviceLocator;
37
38
    protected $event;
39
40
    public function __construct(ServiceLocatorInterface $locator)
41
    {
42
        $this->serviceLocator = $locator;
0 ignored issues
show
Documentation Bug introduced by
$locator is of type object<Zend\ServiceManag...erviceLocatorInterface>, but the property $serviceLocator was declared to be of type object<Zend\ServiceManager\ServiceManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
43
    }
44
45 View Code Duplication
    public function getEventManager() {
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...
46
        if (null === $this->event) {
47
            $this->event = new EventManager($this->serviceLocator->get('SharedEventManager'), [get_class($this)]);
48
        }
49
50
        return $this->event;
51
    }
52
53
    /**
54
     *
55
     * This service is ready for all types of games
56
     *
57
     * @param  array                  $data
58
     * @param  string                 $formClass
59
     * @return \PlaygroundGame\Entity\Game
60
     */
61
    public function create(array $data, $prizeCategory, $formClass)
62
    {
63
        $form  = $this->serviceLocator->get($formClass);
64
        $form->bind($prizeCategory);
65
66
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
67
        $media_url = $this->getOptions()->getMediaUrl() . '/';
68
69
        $form->setData($data);
70
71
        if (!$form->isValid()) {
72
            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\PrizeCategory::create 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...
73
        }
74
75
        $prizeCategory = $this->getPrizeCategoryMapper()->insert($prizeCategory);
76
77 View Code Duplication
        if (!empty($data['upload_picto']['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...
78
            ErrorHandler::start();
79
            move_uploaded_file(
80
                $data['upload_picto']['tmp_name'],
81
                $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name']
82
            );
83
            $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']);
84
            ErrorHandler::stop(true);
85
        }
86
87
        $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory);
88
89
        return $prizeCategory;
90
    }
91
92
    /**
93
     *
94
     * @param  array                  $data
95
     * @param  string                 $formClass
96
     * @return \PlaygroundGame\Entity\Game
97
     */
98
    public function edit(array $data, $prizeCategory, $formClass)
99
    {
100
        $form  = $this->serviceLocator->get($formClass);
101
        $form->bind($prizeCategory);
102
103
        $path = $this->getOptions()->getMediaPath() . DIRECTORY_SEPARATOR;
104
        $media_url = $this->getOptions()->getMediaUrl() . '/';
105
106
        $form->setData($data);
107
108
        if (!$form->isValid()) {
109
            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\PrizeCategory::edit 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...
110
        }
111
112 View Code Duplication
        if (!empty($data['upload_picto']['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...
113
            ErrorHandler::start();
114
            move_uploaded_file(
115
                $data['upload_picto']['tmp_name'],
116
                $path . $prizeCategory->getId() . "-" . $data['upload_picto']['name']
117
            );
118
            $prizeCategory->setPicto($media_url . $prizeCategory->getId() . "-" . $data['upload_picto']['name']);
119
            ErrorHandler::stop(true);
120
        }
121
122
        $prizeCategory = $this->getPrizeCategoryMapper()->update($prizeCategory);
123
124
        return $prizeCategory;
125
    }
126
127
    public function getActivePrizeCategories()
128
    {
129
        $em = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
130
131
        $query = $em->createQuery('SELECT p FROM PlaygroundGame\Entity\PrizeCategory p WHERE p.active = true');
132
        $categories = $query->getResult();
133
134
        return $categories;
135
    }
136
137
    /**
138
     * getPrizeCategoryMapper
139
     *
140
     * @return PrizeCategoryMapper
141
     */
142
    public function getPrizeCategoryMapper()
143
    {
144
        if (null === $this->prizeCategoryMapper) {
145
            $this->prizeCategoryMapper = $this->serviceLocator->get('playgroundgame_prizecategory_mapper');
146
        }
147
148
        return $this->prizeCategoryMapper;
149
    }
150
151
    /**
152
     * setPrizeCategoryMapper
153
     *
154
     * @param  PrizeCategoryMapper $prizeCategoryMapper
155
     * @return PrizeCategory
156
     */
157
    public function setPrizeCategoryMapper(PrizeCategoryMapper $prizeCategoryMapper)
158
    {
159
        $this->prizeCategoryMapper = $prizeCategoryMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prizeCategoryMapper of type object<PlaygroundGame\Mapper\PrizeCategory> is incompatible with the declared type object<PlaygroundGame\Se...ce\prizeCategoryMapper> of property $prizeCategoryMapper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
160
161
        return $this;
162
    }
163
164
    public function setOptions(ModuleOptions $options)
165
    {
166
        $this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type object<PlaygroundGame\Options\ModuleOptions> is incompatible with the declared type object<PlaygroundGame\Se...erviceOptionsInterface> of property $options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
167
168
        return $this;
169
    }
170
171
    public function getOptions()
172
    {
173
        if (!$this->options instanceof ModuleOptions) {
174
            $this->setOptions($this->serviceLocator->get('playgroundgame_module_options'));
175
        }
176
177
        return $this->options;
178
    }
179
180
    /**
181
     * Retrieve service manager instance
182
     *
183
     * @return ServiceManager
184
     */
185
    public function getServiceManager()
186
    {
187
        return $this->serviceManager;
188
    }
189
190
    /**
191
     * Set service manager instance
192
     *
193
     * @param  ServiceManager $serviceManager
194
     * @return PrizeCategory
195
     */
196
    public function setServiceManager(ServiceManager $serviceManager)
197
    {
198
        $this->serviceManager = $serviceManager;
199
200
        return $this;
201
    }
202
}
203