Completed
Push — develop ( f65419...2fe783 )
by
unknown
06:34
created

BaseFieldset   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 5
dl 0
loc 114
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setLocationEngineType() 0 4 1
B getHydrator() 0 35 2
B init() 0 43 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author    [email protected]
9
 */
10
11
namespace Jobs\Form;
12
13
use Core\Entity\EntityInterface;
14
use Core\Entity\EntityTrait;
15
use Core\Entity\Hydrator\MappingEntityHydrator;
16
use Core\Form\HydratorStrategyAwareTrait;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Jobs\Entity\Location;
19
use Jobs\Form\Hydrator\Strategy\LocationStrategy;
20
use Zend\Form\Exception;
21
use Zend\Form\Fieldset;
22
use Core\Entity\Hydrator\EntityHydrator;
23
use Zend\Form\FieldsetInterface;
24
use Core\Form\CustomizableFieldsetInterface;
25
use Core\Form\CustomizableFieldsetTrait;
26
27
/**
28
 * Defines the formular fields of the base formular of a job opening.
29
 */
30
class BaseFieldset extends Fieldset implements CustomizableFieldsetInterface
31
{
32
    use CustomizableFieldsetTrait;
33
34
    /**
35
     * name of the used geo location Engine
36
     *
37
     * @var string  $locationEngineType
38
     */
39
    protected $locationEngineType;
40
41
    /**
42
     * @param array $options
43
     */
44
    public function __construct(array $options = [])
45
    {
46
        parent::__construct();
47
        if (array_key_exists('location_engine_type', $options)) {
48
            $this->locationEngineType = $options['location_engine_type'];
49
        }
50
        $this->setOptions($options);
51
    }
52
53
    /**
54
     * @param $locationEngineType
55
     */
56
    public function setLocationEngineType($locationEngineType)
57
    {
58
        $this->locationEngineType = $locationEngineType;
59
    }
60
61
    /**
62
     * @return \Zend\Hydrator\HydratorInterface
63
     */
64
    public function getHydrator()
65
    {
66
        if (!$this->hydrator) {
67
            $hydrator = new MappingEntityHydrator([
68
                'locations' => 'geoLocation'
69
            ]);
70
71
            $geoLocationStrategy = $this->get('geoLocation')->getHydratorStrategy();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Form\ElementInterface as the method getHydratorStrategy() does only exist in the following implementations of said interface: Core\Form\Tree\Select, Geo\Form\GeoSelect, Geo\Form\GeoSelectSimple.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
72
73
            $locationsStrategy = new \Zend\Hydrator\Strategy\ClosureStrategy(
74
                /* extract */
75
                function ($value) use ($geoLocationStrategy)
76
                {
77
                    return $geoLocationStrategy->extract($value->first());
78
                },
79
80
                /* hydrate */
81
                function ($value) use ($geoLocationStrategy)
82
                {
83
                    return new ArrayCollection([$geoLocationStrategy->hydrate($value)]);
84
                }
85
            );
86
87
            $hydrator->addStrategy('locations', $locationsStrategy);
88
89
            /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
            $datetimeStrategy = new Hydrator\DatetimeStrategy();
91
            $datetimeStrategy->setHydrateFormat(Hydrator\DatetimeStrategy::FORMAT_MYSQLDATE);
92
            $hydrator->addStrategy('datePublishStart', $datetimeStrategy);
93
            */
94
95
            $this->setHydrator($hydrator);
96
        }
97
        return $this->hydrator;
98
    }
99
100
    public function init()
101
    {
102
        $this->setAttribute('id', 'job-fieldset');
103
104
        $this->setName('jobBase');
105
106
        $this->add(
107
            [
108
                'type' => 'Text',
109
                'name' => 'title',
110
                'options' => [
111
                    'label' => /*@translate*/ 'Job title',
112
                    'description' => /*@translate*/ 'Please enter the job title'
113
                ],
114
            ]
115
        );
116
117
118
        $this->add(
119
            [
120
                'type' => 'LocationSelect',
121
                'name' => 'geoLocation',
122
                'options' => [
123
                    'label' => /*@translate*/ 'Location',
124
                    'description' => /*@translate*/ 'Please enter the location of the job',
125
                    'location_entity' => Location::class,
126
                    'summary_value' => function() {
127
                            $loc = $this->object->getLocations()->first();
128
                            if (!$loc) { return ''; }
129
130
                            $value = $loc->getPostalCode() . ' ' . $loc->getCity() . ', ' . $loc->getRegion();
131
                            $value = trim($value, ' ,');
132
133
                            return $value;
134
                    },
135
                ],
136
                'attributes' => [
137
                    'data-width' => '100%',
138
                ]
139
            ]
140
        );
141
142
    }
143
}
144
145