DeliveryMethod   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 28
c 1
b 0
f 0
dl 0
loc 68
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromXML() 0 23 6
A getVisibility() 0 3 1
A setName() 0 3 1
A getName() 0 3 1
A getProducts() 0 3 1
A isVisibleAndActive() 0 3 1
A setVisibility() 0 3 1
A addProduct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Bpost\BpostApiClient\Bpost\ProductConfiguration;
5
6
use SimpleXMLElement;
7
8
/**
9
 * Class DeliveryMethod
10
 */
11
class DeliveryMethod
12
{
13
    public const DELIVERY_METHOD_NAME_HOME_OR_OFFICE  = 'home or office';
14
    public const DELIVERY_METHOD_NAME_PICKUP_POINT    = 'pick-up point';
15
    public const DELIVERY_METHOD_NAME_PARCEL_LOCKER   = 'parcel locker';
16
    public const DELIVERY_METHOD_NAME_CLICK_AND_COLLECT = 'Click & Collect';
17
18
    private string $name;
19
    private string $visibility;
20
    /** @var Product[] */
21
    private array $products = [];
22
23
    public static function createFromXML(SimpleXMLElement $xml): self
24
    {
25
        $attributes = $xml->attributes();
26
        $children   = $xml->children();
27
28
        $instance = new self();
29
        if (isset($attributes['name'])) {
30
            $instance->setName((string) $attributes['name']);
31
        }
32
        // Correction de l'attribut: visibility (et fallback si l’API renvoie l’ancienne coquille)
33
        if (isset($attributes['visibility'])) {
34
            $instance->setVisibility((string) $attributes['visibility']);
35
        } elseif (isset($attributes['visiblity'])) {
36
            $instance->setVisibility((string) $attributes['visiblity']);
37
        }
38
39
        if (isset($children->product)) {
40
            foreach ($children->product as $productXml) {
41
                $instance->addProduct(Product::createFromXML($productXml));
0 ignored issues
show
Bug introduced by
It seems like $productXml can also be of type null; however, parameter $xml of Bpost\BpostApiClient\Bpo...roduct::createFromXML() does only seem to accept SimpleXMLElement, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
                $instance->addProduct(Product::createFromXML(/** @scrutinizer ignore-type */ $productXml));
Loading history...
42
            }
43
        }
44
45
        return $instance;
46
    }
47
48
    public function getName(): string
49
    {
50
        return $this->name;
51
    }
52
    public function setName(string $name): void
53
    {
54
        $this->name = $name;
55
    }
56
57
    public function getVisibility(): string
58
    {
59
        return $this->visibility;
60
    }
61
62
    public function setVisibility(string $visibility): void
63
    {
64
        $this->visibility = $visibility;
65
    }
66
67
    public function isVisibleAndActive(): bool
68
    {
69
        return $this->getVisibility() === Visibility::DELIVERY_METHOD_VISIBILITY_VISIBLE;
70
    }
71
72
    public function getProducts(): array
73
    {
74
        return $this->products;
75
    }
76
    public function addProduct(Product $product): void
77
    {
78
        $this->products[] = $product;
79
    }
80
}