Tweets   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 67
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A createFromArray() 0 17 4
A getMessage() 0 4 1
A getTweets() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
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 FAPI\Boilerplate\Model\Tweet;
11
12
use FAPI\Boilerplate\Exception\InvalidArgumentException;
13
use FAPI\Boilerplate\Model\CreatableFromArray;
14
15
/**
16
 * @author Tobias Nyholm <[email protected]>
17
 */
18
final class Tweets implements CreatableFromArray
19
{
20
    /**
21
     * @var string
22
     */
23
    private $message;
24
25
    /**
26
     * @var Tweet[]
27
     */
28
    private $tweets;
29
30
    /**
31
     * @param string  $message
32
     * @param Tweet[] $tweets
33
     */
34
    private function __construct(string $message, array $tweets)
35
    {
36
        foreach ($tweets as $tweet) {
37
            if (!$tweet instanceof Tweet) {
38
                throw new InvalidArgumentException('All tweets must be an instance of '.Tweet::class);
39
            }
40
        }
41
42
        $this->message = $message;
43
        $this->tweets = $tweets;
44
    }
45
46
    /**
47
     * @param array $data
48
     *
49
     * @return Tweets
50
     */
51
    public static function createFromArray(array $data): Tweets
52
    {
53
        $message = '';
54
        $tweets = [];
55
56
        if (isset($data['tweets'])) {
57
            foreach ($data['tweets'] as $item) {
58
                $tweets[] = Tweet::createFromArray($item);
59
            }
60
        }
61
62
        if (isset($data['message'])) {
63
            $message = $data['message'];
64
        }
65
66
        return new self($message, $tweets);
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getMessage(): string
73
    {
74
        return $this->message;
75
    }
76
77
    /**
78
     * @return Tweet[]
79
     */
80
    public function getTweets(): array
81
    {
82
        return $this->tweets;
83
    }
84
}
85