ProductUom   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 64.04%

Importance

Changes 6
Bugs 3 Features 4
Metric Value
wmc 29
lcom 2
cbo 3
dl 0
loc 162
ccs 57
cts 89
cp 0.6404
rs 10
c 6
b 3
f 4

11 Methods

Rating   Name   Duplication   Size   Complexity  
A cheapestUom() 0 14 4
A getByProductId() 0 10 3
C populate() 0 23 9
A getAvailabilityService() 0 7 2
A setAvailabilityService() 0 5 1
A getUomService() 0 7 2
A setUomService() 0 5 1
A update() 0 16 1
B insert() 0 24 3
A getProductService() 0 7 2
A setProductService() 0 5 1
1
<?php
2
3
namespace SpeckCatalog\Service;
4
5
use SpeckCatalog\Model\AbstractModel;
6
7
class ProductUom extends AbstractService
8
{
9
    protected $entityMapper = 'speckcatalog_product_uom_mapper';
10
    protected $availabilityService;
11
    protected $uomService;
12
    protected $productService;
13
14 1
    public function getByProductId($productId, $populate = false, $recursive = false)
15
    {
16 1
        $productUoms = $this->getEntityMapper()->getByProductId($productId);
17 1
        if ($populate) {
18 1
            foreach ($productUoms as $productUom) {
19 1
                $this->populate($productUom, $recursive);
20 1
            }
21 1
        }
22 1
        return $productUoms;
23
    }
24
25
    public function cheapestUom(array $uoms)
26
    {
27
        $lowest = null;
28
        foreach ($uoms as $uom) {
29
            if (!$lowest) {
30
                $lowest = $uom;
31
            }
32
            if ($uom->getPrice() < $lowest->getPrice()) {
33
                $lowest = $uom;
34
            }
35
        }
36
37
        return $lowest;
38
    }
39
40 2
    public function populate($productUom, $recursive = false, $children = true)
41
    {
42 2
        $allChildren = ($children === true) ? true : false;
43 2
        $children    = (is_array($children)) ? $children : array();
44
45 2
        if ($allChildren || in_array('availabilities', $children)) {
46 2
            $availabilities = $this->getAvailabilityService()->getByProductUom(
47 2
                $productUom->getProductId(),
48 2
                $productUom->getUomCode(),
49 2
                $productUom->getQuantity()
50 2
            );
51 2
            if ($recursive) {
52
                foreach ($availabilities as $i => $avail) {
53
                    $this->getAvailabilityService()->populate($avail);
54
                }
55
            }
56 2
            $productUom->setAvailabilities($availabilities);
57 2
        }
58 2
        if ($allChildren || in_array('uom', $children)) {
59 2
            $uom = $this->getUomService()->find(array('uom_code' => $productUom->getUomCode()));
60 2
            $productUom->setUom($uom);
61 2
        }
62 2
    }
63
64
    /**
65
     * @return availabilityService
66
     */
67 4
    public function getAvailabilityService()
68
    {
69 4
        if (null === $this->availabilityService) {
70 1
            $this->availabilityService = $this->getServiceLocator()->get('speckcatalog_availability_service');
71 1
        }
72 4
        return $this->availabilityService;
73
    }
74
75
    /**
76
     * @param $availabilityService
77
     * @return self
78
     */
79 3
    public function setAvailabilityService($availabilityService)
80
    {
81 3
        $this->availabilityService = $availabilityService;
82 3
        return $this;
83
    }
84
85
    /**
86
     * @return uomService
87
     */
88 4
    public function getUomService()
89
    {
90 4
        if (null === $this->uomService) {
91 1
            $this->uomService = $this->getServiceLocator()->get('speckcatalog_uom_service');
92 1
        }
93 4
        return $this->uomService;
94
    }
95
96
    /**
97
     * @param $uomService
98
     * @return self
99
     */
100 3
    public function setUomService($uomService)
101
    {
102 3
        $this->uomService = $uomService;
103 3
        return $this;
104
    }
105
106
    public function update($data, array $where = null)
107
    {
108
        $vars = array(
109
            'data'        => $data,
110
            'where'       => $where,
111
        );
112
113
        $this->getEventManager()->trigger('update.pre', $this, $vars);
114
115
        $result = parent::update($data, $where);
116
        $vars['result'] = $result;
117
118
        $this->getEventManager()->trigger('update.post', $this, $vars);
119
120
        return $result;
121
    }
122
123 2
    public function insert($productUom)
124
    {
125 2
        if ($productUom instanceof AbstractModel) {
126
            $data = array(
127 1
                'uom_code' => $productUom->getUomCode(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SpeckCatalog\Model\AbstractModel as the method getUomCode() does only exist in the following sub-classes of SpeckCatalog\Model\AbstractModel: SpeckCatalog\Model\Availability, SpeckCatalog\Model\Availability\Relational, SpeckCatalog\Model\ProductUom, SpeckCatalog\Model\ProductUom\Relational, SpeckCatalog\Model\Uom, SpeckCatalog\Model\Uom\Relational. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
128 1
                'product_id' => $productUom->getProductId(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SpeckCatalog\Model\AbstractModel as the method getProductId() does only exist in the following sub-classes of SpeckCatalog\Model\AbstractModel: SpeckCatalog\Model\Availability, SpeckCatalog\Model\Availability\Relational, SpeckCatalog\Model\Builder\Relational, SpeckCatalog\Model\Choice, SpeckCatalog\Model\Choice\Relational, SpeckCatalog\Model\Document, SpeckCatalog\Model\Document\Relational, SpeckCatalog\Model\Feature, SpeckCatalog\Model\Feature\Relational, SpeckCatalog\Model\Option\Relational, SpeckCatalog\Model\Product, SpeckCatalog\Model\ProductImage, SpeckCatalog\Model\ProductImage\Relational, SpeckCatalog\Model\ProductUom, SpeckCatalog\Model\ProductUom\Relational, SpeckCatalog\Model\Product\Relational, SpeckCatalog\Model\Spec, SpeckCatalog\Model\Spec\Relational. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
129 1
                'quantity' => $productUom->getQuantity(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class SpeckCatalog\Model\AbstractModel as the method getQuantity() does only exist in the following sub-classes of SpeckCatalog\Model\AbstractModel: SpeckCatalog\Model\Availability, SpeckCatalog\Model\Availability\Relational, SpeckCatalog\Model\ProductUom, SpeckCatalog\Model\ProductUom\Relational. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
130 1
            );
131 2
        } elseif (is_array($productUom)) {
132 1
            $data = $productUom;
133 1
        }
134
135
        $vars = array(
136 2
            'data' => $data,
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
137 2
        );
138 2
        $this->getEventManager()->trigger('insert.pre', $this, $vars);
139
140 2
        $vars['result'] = parent::insert($productUom);
141
142 2
        $this->getEventManager()->trigger('insert.post', $this, $vars);
143
144 2
        $productUom = $this->find($data);
145 2
        return $productUom;
146
    }
147
148
    /**
149
     * @return productService
150
     */
151
    public function getProductService()
152
    {
153
        if (null === $this->productService) {
154
            $this->productService = $this->getServiceLocator()->get('speckcatalog_product_service');
155
        }
156
        return $this->productService;
157
    }
158
159
    /**
160
     * @param $productService
161
     * @return self
162
     */
163
    public function setProductService($productService)
164
    {
165
        $this->productService = $productService;
166
        return $this;
167
    }
168
}
169