Passed
Pull Request — master (#102)
by
unknown
04:33 queued 02:09
created

BillingRegistryService::hasBehaviour()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace hiqdev\php\billing\product\Application;
4
5
use hiqdev\php\billing\product\AggregateInterface;
6
use hiqdev\php\billing\product\behavior\BehaviorInterface;
7
use hiqdev\php\billing\product\behavior\BehaviorNotFoundException;
8
use hiqdev\php\billing\product\behavior\InvalidBehaviorException;
9
use hiqdev\php\billing\product\BillingRegistryInterface;
10
use hiqdev\php\billing\product\Exception\AggregateNotFoundException;
11
use hiqdev\php\billing\product\Exception\TariffTypeDefinitionNotFoundException;
12
use hiqdev\php\billing\product\invoice\InvalidRepresentationException;
13
use hiqdev\php\billing\product\invoice\RepresentationInterface;
14
use hiqdev\php\billing\product\price\PriceTypeDefinitionInterface;
15
use hiqdev\php\billing\product\quantity\FractionQuantityData;
16
use hiqdev\php\billing\product\quantity\QuantityFormatterInterface;
17
use hiqdev\php\billing\product\quantity\QuantityFormatterNotFoundException;
18
use hiqdev\php\billing\product\TariffTypeDefinitionInterface;
19
use hiqdev\php\billing\type\Type;
20
use hiqdev\php\billing\type\TypeInterface;
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
final class BillingRegistryService implements BillingRegistryServiceInterface
23
{
24
    public function __construct(private readonly BillingRegistryInterface $registry)
25
    {
26
    }
27
28
    public function getRepresentationsByType(string $representationClass): array
29
    {
30
        if (!class_exists($representationClass) && !interface_exists($representationClass)) {
31
            throw new InvalidRepresentationException("Class '$representationClass' does not exist");
32
        }
33
34
        if (class_exists($representationClass) && !is_subclass_of($representationClass, RepresentationInterface::class)) {
35
            throw new InvalidBehaviorException(
36
                sprintf('Representation class "%s" does not implement RepresentationInterface', $representationClass)
37
            );
38
        }
39
40
        $representations = [];
41
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
42
            foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
43
                if ($representation instanceof $representationClass) {
44
                    $representations[] = $representation;
45
                }
46
            }
47
        }
48
49
        return $representations;
50
    }
51
52
    public function getTariffTypeDefinitionByName(string $tariffName): TariffTypeDefinitionInterface
53
    {
54
        foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
55
            if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) {
56
                return $tariffTypeDefinition;
57
            }
58
        }
59
60
        throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
61
    }
62
63
    public function createQuantityFormatter(string $type, FractionQuantityData $data): QuantityFormatterInterface {
64
        $type = $this->convertStringTypeToType($type);
65
66
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
67
            if ($priceTypeDefinition->hasType($type)) {
68
                return $priceTypeDefinition->createQuantityFormatter($data);
69
            }
70
        }
71
72
        throw new QuantityFormatterNotFoundException('Quantity formatter not found');
73
    }
74
75
    private function convertStringTypeToType(string $type): TypeInterface
76
    {
77
        return Type::anyId($type);
78
    }
79
80
    public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
81
    {
82
        if (!class_exists($behaviorClassWrapper)) {
83
            throw new InvalidBehaviorException(
84
                sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
85
            );
86
        }
87
88
        if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
89
            throw new InvalidBehaviorException(
90
                sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
91
            );
92
        }
93
94
        $billingType = $this->convertStringTypeToType($type);
95
96
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
97
            if ($priceTypeDefinition->hasType($billingType)) {
98
                $behavior = $this->findBehaviorInPriceType($priceTypeDefinition, $behaviorClassWrapper);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $behavior is correct as $this->findBehaviorInPri... $behaviorClassWrapper) targeting hiqdev\php\billing\produ...ndBehaviorInPriceType() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
99
100
                if ($behavior) {
101
                    return $behavior;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $behavior returns the type void which is incompatible with the type-hinted return hiqdev\php\billing\produ...avior\BehaviorInterface.
Loading history...
102
                }
103
            }
104
        }
105
106
        throw new BehaviorNotFoundException(
107
            sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type),
108
        );
109
    }
110
111
    private function findBehaviorInPriceType(
112
        PriceTypeDefinitionInterface $priceTypeDefinition,
113
        string $behaviorClassWrapper
114
    ): ?BehaviorInterface {
115
        foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
116
            if ($behavior instanceof $behaviorClassWrapper) {
117
                return $behavior;
118
            }
119
        }
120
121
        return null;
122
    }
123
124
    public function getBehaviors(string $behaviorClassWrapper): \Generator
125
    {
126
        foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
127
            foreach ($tariffTypeDefinition->withBehaviors() as $behavior) {
128
                if ($behavior instanceof $behaviorClassWrapper) {
129
                    yield $behavior;
130
                }
131
            }
132
        }
133
134
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
135
            foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
136
                if ($behavior instanceof $behaviorClassWrapper) {
137
                    yield $behavior;
138
                }
139
            }
140
        }
141
    }
142
143
    public function getAggregate(string $type): AggregateInterface
144
    {
145
        $type = $this->convertStringTypeToType($type);
146
147
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
148
            if ($priceTypeDefinition->hasType($type)) {
149
                return $priceTypeDefinition->getAggregate();
150
            }
151
        }
152
153
        throw new AggregateNotFoundException('Aggregate was not found');
154
    }
155
156
    public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
157
    {
158
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
159
            if ($priceTypeDefinition->hasBehavior($behaviorClassWrapper)) {
160
                yield $priceTypeDefinition;
161
            }
162
        }
163
    }
164
}
165