Prize::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PlaygroundGame\Service;
4
5
use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
6
use Zend\ServiceManager\ServiceManager;
7
use Zend\EventManager\EventManagerAwareTrait;
8
use PlaygroundGame\Options\ModuleOptions;
9
use PlaygroundGame\Mapper\Prize as PrizeMapper;
10
use Zend\ServiceManager\ServiceLocatorInterface;
11
12
class Prize
13
{
14
    use EventManagerAwareTrait;
15
16
    /**
17
     * @var prizeMapper
18
     */
19
    protected $prizeMapper;
20
21
    /**
22
     * @var ServiceManager
23
     */
24
    protected $serviceManager;
25
26
    /**
27
     * @var UserServiceOptionsInterface
28
     */
29
    protected $options;
30
31
    /**
32
     *
33
     * @var ServiceManager
34
     */
35
    protected $serviceLocator;
36
37
    public function __construct(ServiceLocatorInterface $locator)
38
    {
39
        $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...
40
    }
41
42
    /**
43
     *
44
     * This service is ready for all types of games
45
     *
46
     * @param  array                  $data
47
     * @param  string                 $formClass
48
     * @return \PlaygroundGame\Entity\Game
49
     */
50
    public function create(array $data, $prize, $formClass)
51
    {
52
        $form  = $this->serviceLocator->get($formClass);
53
        $form->bind($prize);
54
55
        // If the identifier has not been set, I use the title to create one.
56 View Code Duplication
        if (empty($data['identifier']) && !empty($data['title'])) {
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...
57
            $data['identifier'] = $data['title'];
58
        }
59
        
60
        $form->setData($data);
61
62
        if (!$form->isValid()) {
63
            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\Prize::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...
64
        }
65
66
        $prize = $this->getPrizeMapper()->insert($prize);
67
68
        return $prize;
69
    }
70
71
    /**
72
     *
73
     * @param  array                  $data
74
     * @param  string                 $formClass
75
     * @return \PlaygroundGame\Entity\Game
76
     */
77
    public function edit(array $data, $prize, $formClass)
78
    {
79
        $entityManager = $this->serviceLocator->get('doctrine.entitymanager.orm_default');
80
        $form  = $this->serviceLocator->get($formClass);
81
        $form->bind($prize);
82
        
83
        $identifierInput = $form->getInputFilter()->get('identifier');
84
        $noObjectExistsValidator = new NoObjectExistsValidator(array(
85
            'object_repository' => $entityManager->getRepository('PlaygroundGame\Entity\Prize'),
86
            'fields'            => 'identifier',
87
            'messages'          => array('objectFound' => 'This url already exists !')
88
        ));
89
        
90
        if ($prize->getIdentifier() != $data['identifier']) {
91
            $identifierInput->getValidatorChain()->addValidator($noObjectExistsValidator);
92
        }
93
94
        $form->setData($data);
95
96
        if (!$form->isValid()) {
97
            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\Prize::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...
98
        }
99
100
        $prize = $this->getPrizeMapper()->update($prize);
101
102
        return $prize;
103
    }
104
105
106
    /**
107
     * getPrizeMapper
108
     *
109
     * @return PrizeMapper
110
     */
111 View Code Duplication
    public function getPrizeMapper()
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...
112
    {
113
        if (null === $this->prizeMapper) {
114
            $this->prizeMapper = $this->serviceLocator->get('playgroundgame_prize_mapper');
115
        }
116
117
        return $this->prizeMapper;
118
    }
119
120
    /**
121
     * setPrizeMapper
122
     *
123
     * @param  PrizeMapper $prizeMapper
124
     * @return Prize
125
     */
126
    public function setPrizeMapper(PrizeMapper $prizeMapper)
127
    {
128
        $this->prizeMapper = $prizeMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prizeMapper of type object<PlaygroundGame\Mapper\Prize> is incompatible with the declared type object<PlaygroundGame\Service\prizeMapper> of property $prizeMapper.

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...
129
130
        return $this;
131
    }
132
133
    public function setOptions(ModuleOptions $options)
134
    {
135
        $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...
136
137
        return $this;
138
    }
139
140
    public function getOptions()
141
    {
142
        if (!$this->options instanceof ModuleOptions) {
143
            $this->setOptions($this->serviceLocator->get('playgroundgame_module_options'));
144
        }
145
146
        return $this->options;
147
    }
148
149
    /**
150
     * Retrieve service manager instance
151
     *
152
     * @return ServiceManager
153
     */
154
    public function getServiceManager()
155
    {
156
        return $this->serviceManager;
157
    }
158
159
    /**
160
     * Set service manager instance
161
     *
162
     * @param  ServiceManager $serviceManager
163
     * @return Prize
164
     */
165
    public function setServiceManager(ServiceManager $serviceManager)
166
    {
167
        $this->serviceManager = $serviceManager;
168
169
        return $this;
170
    }
171
}
172