Passed
Pull Request — master (#102)
by
unknown
03:23 queued 01:02
created

getTariffDefinitionByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 8
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\PriceTypeDefinitionNotFoundException;
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\TariffTypeDefinitionInterface;
16
use hiqdev\php\billing\type\Type;
17
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...
18
19
final class BillingRegistryService implements BillingRegistryServiceInterface
20
{
21
    public function __construct(private readonly BillingRegistryInterface $registry)
22
    {
23
    }
24
25
    public function getRepresentationsByType(string $representationClass): array
26
    {
27
        if (!class_exists($representationClass) && !interface_exists($representationClass)) {
28
            throw new InvalidRepresentationException("Class '$representationClass' does not exist");
29
        }
30
31
        if (class_exists($representationClass)
32
            && !is_subclass_of($representationClass, RepresentationInterface::class)
33
        ) {
34
            throw new InvalidBehaviorException(
35
                sprintf(
36
                    'Representation class "%s" does not implement RepresentationInterface',
37
                    $representationClass,
38
                )
39
            );
40
        }
41
42
        $representations = [];
43
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
44
            foreach ($priceTypeDefinition->documentRepresentation() as $representation) {
45
                if ($representation instanceof $representationClass) {
46
                    $representations[] = $representation;
47
                }
48
            }
49
        }
50
51
        return $representations;
52
    }
53
54
    public function getTariffTypeDefinitionByTariffName(string $tariffName): TariffTypeDefinitionInterface
55
    {
56
        foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
57
            if ($tariffTypeDefinition->tariffType()->equalsName($tariffName)) {
58
                return $tariffTypeDefinition;
59
            }
60
        }
61
62
        throw new TariffTypeDefinitionNotFoundException('Tariff type definition was not found');
63
    }
64
65
    public function getBehavior(string $type, string $behaviorClassWrapper): BehaviorInterface
66
    {
67
        if (!class_exists($behaviorClassWrapper)) {
68
            throw new InvalidBehaviorException(
69
                sprintf('Behavior class "%s" does not exist', $behaviorClassWrapper)
70
            );
71
        }
72
73
        if (!is_subclass_of($behaviorClassWrapper, BehaviorInterface::class)) {
74
            throw new InvalidBehaviorException(
75
                sprintf('Behavior class "%s" does not implement BehaviorInterface', $behaviorClassWrapper)
76
            );
77
        }
78
79
        $billingType = $this->convertStringTypeToType($type);
80
81
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
82
            if ($priceTypeDefinition->hasType($billingType)) {
83
                $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...
84
85
                if ($behavior) {
86
                    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...
87
                }
88
            }
89
        }
90
91
        throw new BehaviorNotFoundException(
92
            sprintf('Behavior of class "%s" not found for type "%s"', $behaviorClassWrapper, $type),
93
        );
94
    }
95
96
    private function convertStringTypeToType(string $type): TypeInterface
97
    {
98
        return Type::anyId($type);
99
    }
100
101
    private function findBehaviorInPriceType(
102
        PriceTypeDefinitionInterface $priceTypeDefinition,
103
        string $behaviorClassWrapper
104
    ): ?BehaviorInterface {
105
        foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
106
            if ($behavior instanceof $behaviorClassWrapper) {
107
                return $behavior;
108
            }
109
        }
110
111
        return null;
112
    }
113
114
    public function getBehaviors(string $behaviorClassWrapper): \Generator
115
    {
116
        foreach ($this->registry->getTariffTypeDefinitions() as $tariffTypeDefinition) {
117
            foreach ($tariffTypeDefinition->withBehaviors() as $behavior) {
118
                if ($behavior instanceof $behaviorClassWrapper) {
119
                    yield $behavior;
120
                }
121
            }
122
        }
123
124
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
125
            foreach ($priceTypeDefinition->withBehaviors() as $behavior) {
126
                if ($behavior instanceof $behaviorClassWrapper) {
127
                    yield $behavior;
128
                }
129
            }
130
        }
131
    }
132
133
    public function getAggregate(string $type): AggregateInterface
134
    {
135
        return $this->getPriceTypeDefinitionByPriceTypeName($type)->getAggregate();
136
    }
137
138
    public function getPriceTypeDefinitionByPriceTypeName(string $typeName): PriceTypeDefinitionInterface
139
    {
140
        $type = $this->convertStringTypeToType($typeName);
141
142
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
143
            if ($priceTypeDefinition->hasType($type)) {
144
                return $priceTypeDefinition;
145
            }
146
        }
147
148
        throw new PriceTypeDefinitionNotFoundException(sprintf(
149
            'PriceTypeDefinition was not found for %s type',
150
            $typeName,
151
        ));
152
    }
153
154
    public function findPriceTypeDefinitionsByBehavior(string $behaviorClassWrapper): \Generator
155
    {
156
        foreach ($this->registry->priceTypes() as $priceTypeDefinition) {
157
            if ($priceTypeDefinition->hasBehavior($behaviorClassWrapper)) {
158
                yield $priceTypeDefinition;
159
            }
160
        }
161
    }
162
}
163