Completed
Push — master ( da873d...74929b )
by Tobias
03:47
created

TotalResponse::create()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 11
cp 0
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 16
nop 1
crap 42
1
<?php
2
3
/*
4
 * Copyright (C) 2013-2016 Mailgun
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace Mailgun\Model\Stats;
11
12
use Mailgun\Model\ApiResponse;
13
14
/**
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
final class TotalResponse implements ApiResponse
18
{
19
    /**
20
     * @var \DateTime
21
     */
22
    private $start;
23
24
    /**
25
     * @var \DateTime
26
     */
27
    private $end;
28
29
    /**
30
     * @var string
31
     */
32
    private $resolution;
33
34
    /**
35
     * @var TotalResponseItem[]
36
     */
37
    private $stats;
38
39
    /**
40
     * @param \DateTime           $start
41
     * @param \DateTime           $end
42
     * @param string              $resolution
43
     * @param TotalResponseItem[] $stats
44
     */
45
    private function __construct(\DateTime $start, \DateTime $end, $resolution, array $stats)
46
    {
47
        $this->start = $start;
48
        $this->end = $end;
49
        $this->resolution = $resolution;
50
        $this->stats = $stats;
51
    }
52
53
    /**
54
     * @param array $data
55
     *
56
     * @return self
57
     */
58
    public static function create(array $data)
59
    {
60
        $stats = [];
61
        if (isset($data['status'])) {
62
            foreach ($data['stats'] as $s) {
63
                $stats[] = TotalResponseItem::create($s);
64
            }
65
        }
66
67
        $start = isset($data['start']) ? new \DateTime($data['start']) : null;
68
        $end = isset($data['end']) ? new \DateTime($data['end']) : null;
69
        $resolution = isset($data['resolution']) ? $data['resolution'] : null;
70
71
        return new self($start, $end, $resolution, $stats);
0 ignored issues
show
Bug introduced by
It seems like $start defined by isset($data['start']) ? ...($data['start']) : null on line 67 can be null; however, Mailgun\Model\Stats\TotalResponse::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $end defined by isset($data['end']) ? ne...me($data['end']) : null on line 68 can be null; however, Mailgun\Model\Stats\TotalResponse::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
72
    }
73
74
    /**
75
     * @return \DateTime
76
     */
77
    public function getStart()
78
    {
79
        return $this->start;
80
    }
81
82
    /**
83
     * @return \DateTime
84
     */
85
    public function getEnd()
86
    {
87
        return $this->end;
88
    }
89
90
    /**
91
     * @return string
92
     */
93
    public function getResolution()
94
    {
95
        return $this->resolution;
96
    }
97
98
    /**
99
     * @return TotalResponseItem[]
100
     */
101
    public function getStats()
102
    {
103
        return $this->stats;
104
    }
105
}
106