Issues (5)

src/Builder/AverageRateBuilder.php (1 issue)

1
<?php
2
3
namespace Movavi\Builder;
4
5
use Movavi\Entity\Rate;
6
use Movavi\Exception\EmptyRateListException;
7
use Movavi\Exception\WrongClassException;
0 ignored issues
show
The type Movavi\Exception\WrongClassException 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...
8
9
/**
10
 * Class AverageRateBuilder
11
 *
12
 * Builder for the Movavi\Entity\Rate
13
 * Implementation of Builder pattern (GoF)
14
 *
15
 * @package Movavi\Builder
16
 */
17
class AverageRateBuilder
18
{
19
    /**
20
     * Returns an instance of Movavi\Entity\Rate,
21
     * that contains average rate from given array
22
     *
23
     * @param array $rates
24
     *
25
     * @return Rate
26
     *
27
     * @throws EmptyRateListException
28
     */
29
    public function fromArray(array $rates): Rate
30
    {
31
        if (empty($rates)) {
32
            throw new EmptyRateListException();
33
        }
34
35
        $ratesSum = array_sum(
36
            array_map(
37
                function(Rate $rate): float
38
                {
39
                    return $rate->getRate();
40
                },
41
                $rates
42
            )
43
        );
44
45
        $averageRate = $ratesSum / count($rates);
46
        $firstRate = array_shift($rates);
47
48
        return new Rate(
49
            $firstRate->getCurrencyFrom(),
50
            $firstRate->getCurrencyTo(),
51
            $firstRate->getDate(),
52
            $averageRate
53
        );
54
    }
55
}
56