Context   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 6
c 1
b 0
f 0
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 3 1
A changeStrategy() 0 5 1
1
<?php
2
3
namespace Tleckie\DesignPatterns\Strategy;
4
5
/**
6
 * Class Context
7
 *
8
 * @package Tleckie\DesignPatterns\Strategy
9
 * @author  Teodoro Leckie Westberg <[email protected]>
10
 */
11
class Context implements ContextInterface
12
{
13
    /** @var StrategyInterface */
14
    private StrategyInterface $strategy;
15
16
    /**
17
     * Context constructor.
18
     *
19
     * @param StrategyInterface $strategy
20
     */
21
    public function __construct(StrategyInterface $strategy)
22
    {
23
        $this->changeStrategy($strategy);
24
    }
25
26
    /**
27
     * @param StrategyInterface $strategy
28
     * @return ContextInterface
29
     */
30
    public function changeStrategy(StrategyInterface $strategy): ContextInterface
31
    {
32
        $this->strategy = $strategy;
33
34
        return $this;
35
    }
36
37
    /**
38
     * @param TermsInterface $val
39
     * @return int|float
40
     */
41
    public function execute(TermsInterface $val): int|float
42
    {
43
        return $this->strategy->calculate($val);
44
    }
45
}
46