Passed
Push — master ( 191f59...1ee7f2 )
by Aleksandr
01:40
created

OfferPackage::getOffersById()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
4
namespace Zenwalker\CommerceML\Model;
5
6
7
/**
8
 * Class OfferPackage
9
 *
10
 * @package Zenwalker\CommerceML\Model
11
 * @property Offer[] offers
12
 */
13
class OfferPackage extends Simple
14
{
15
    /**
16
     * @var Offer[]
17
     */
18
    protected $offers = [];
19
20
    /**
21
     * @var Simple[] array
22
     */
23
    protected $priceTypes = [];
24
25 8
    public function loadXml()
26
    {
27 8
        if ($this->owner->offersXml) {
28 8
            return $this->owner->offersXml->ПакетПредложений;
29
        }
30
31
        return null;
32
    }
33
34
    /**
35
     * @return Offer[]
36
     */
37 1
    public function getOffers()
38
    {
39 1
        if (!$this->offers && $this->xml && $this->xml->Предложения) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->offers of type Zenwalker\CommerceML\Model\Offer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
40 1
            foreach ($this->xml->Предложения->Предложение as $offer) {
41 1
                $this->offers[] = new Offer($this->owner, $offer);
42
            }
43
        }
44 1
        return $this->offers;
45
    }
46
47
    /**
48
     * @return Simple[]
49
     */
50
    public function getPriceTypes()
51
    {
52
        if (!$this->priceTypes && $this->xml) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->priceTypes of type Zenwalker\CommerceML\Model\Simple[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
            foreach ($this->xpath('//c:ТипыЦен/c:ТипЦены') as $type) {
54
                $this->priceTypes[] = new Simple($this->owner, $type);
55
            }
56
        }
57
        return $this->priceTypes;
58
    }
59
60
    /**
61
     * @param $id
62
     * @return null|Offer
63
     * @deprecated will removed in 0.3.0
64
     */
65
    public function getOfferById($id)
66
    {
67
        foreach ($this->getOffers() as $offer) {
68
            if ($offer->getClearId() == $id) {
69
                return $offer;
70
            }
71
        }
72
        return null;
73
    }
74
75
    /**
76
     * @param $id
77
     * @return Offer[]
78
     */
79 1
    public function getOffersById($id)
80
    {
81 1
        $result = [];
82 1
        foreach ($this->getOffers() as $offer) {
83 1
            if ($offer->getClearId() === $id) {
84 1
                $result[] = $offer;
85
            }
86
        }
87 1
        return $result;
88
    }
89
}