Report::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Ionut\Sylar;
4
5
6
use Ionut\Sylar\Filters\FilterReportInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
/**
10
 * Sums all the FilterReports for the current request.
11
 */
12
class Report
13
{
14
15
    /**
16
     * @var FilterReportInterface[]
17
     */
18
    protected $filterReports;
19
20
    /**
21
     * @var ServerRequestInterface
22
     */
23
    protected $request;
24
25
    /**
26
     * @param  FilterReportInterface[]  $filterReports
27
     * @param  ServerRequestInterface   $request
28
     */
29
    public function __construct(array $filterReports, ServerRequestInterface $request)
30
    {
31
        $this->filterReports = $filterReports;
32
        $this->request = $request;
33
    }
34
35
    /**
36
     * Calculate the total impact of the contained filter reports.
37
     *
38
     * @return int
39
     */
40
    public function getTotalImpact()
41
    {
42
        return array_reduce($this->filterReports, function($carry, FilterReportInterface $filterReport) {
43
            return $carry + $filterReport->getImpact();
44
        });
45
    }
46
47
    /**
48
     * Export the report to a human-readable string.
49
     *
50
     * @return string
51
     */
52
    public function __toString()
53
    {
54
        $datetime = (new \DateTime())->format(\DateTime::ISO8601);
55
56
        $filterReports = implode("\n\n", $this->filterReports);
57
        return "[$datetime] {$this->request->getUri()}\n\n{$filterReports}";
58
    }
59
}