TweetCollection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 14
c 1
b 0
f 1
lcom 1
cbo 1
dl 0
loc 148
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A retweets() 0 6 1
A replies() 0 6 1
A before() 0 8 1
A after() 0 4 1
A between() 0 10 2
A contains() 0 6 1
A containsInUrl() 0 8 1
A count() 0 4 1
A get() 0 4 1
A filter() 0 6 1
A formatDate() 0 4 2
1
<?php namespace Tuiter;
2
3
/**
4
 * Class TweetCollection
5
 * @package Tuiter
6
 */
7
final class TweetCollection
8
{
9
    /**
10
     * @var Tweet[]
11
     */
12
    private $tweets;
13
14
    /**
15
     * @param Tweet[] $tweets
16
     */
17
    public function __construct(array $tweets)
18
    {
19
        $this->tweets = $tweets;
20
    }
21
22
    /**
23
     * @param bool $are
24
     *
25
     * @return TweetCollection
26
     */
27
    public function retweets($are = true)
28
    {
29
        return $this->filter(function (Tweet $tweet) use ($are) {
30
            return $are === $tweet->isRetweet();
31
        });
32
    }
33
34
    /**
35
     * @param bool $are
36
     *
37
     * @return TweetCollection
38
     */
39
    public function replies($are = true)
40
    {
41
        return $this->filter(function (Tweet $tweet) use ($are) {
42
            return $are === $tweet->isReply();
43
        });
44
    }
45
46
    /**
47
     * @param $date
48
     * @param bool $show
49
     *
50
     * @return TweetCollection
51
     */
52
    public function before($date, $show = true)
53
    {
54
        $date = $this->formatDate($date);
55
56
        return $this->filter(function (Tweet $tweet) use ($date, $show) {
57
            return $show === $tweet->createdAt < $date;
58
        });
59
    }
60
61
    /**
62
     * @param $date
63
     *
64
     * @return TweetCollection
65
     */
66
    public function after($date)
67
    {
68
        return $this->before($date, false);
69
    }
70
71
    /**
72
     * @param $startDate
73
     * @param $endDate
74
     *
75
     * @return TweetCollection
76
     */
77
    public function between($startDate, $endDate)
78
    {
79
        $startDate = $this->formatDate($startDate);
80
        $endDate = $this->formatDate($endDate);
81
82
        return $this->filter(function (Tweet $tweet) use ($startDate, $endDate) {
83
            return ($tweet->createdAt > $startDate)
84
                && ($tweet->createdAt < $endDate);
85
        });
86
    }
87
88
    /**
89
     * @param $text
90
     * @param bool $contains
91
     *
92
     * @return TweetCollection
93
     */
94
    public function contains($text, $contains = true)
95
    {
96
        return $this->filter(function (Tweet $tweet) use ($text, $contains) {
97
            return $contains === (bool) strpos($tweet->text, $text);
98
        });
99
    }
100
101
    /**
102
     * @param $text
103
     * @param bool $contains
104
     *
105
     * @return TweetCollection
106
     */
107
    public function containsInUrl($text, $contains = true)
108
    {
109
        return $this->filter(function (Tweet $tweet) use ($text, $contains) {
110
            $urls = implode(',', $tweet->expandedUrls);
111
112
            return $contains === (bool) strpos($urls, $text);
113
        });
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function count()
120
    {
121
        return count($this->tweets);
122
    }
123
124
    /**
125
     * @return Tweet[]
126
     */
127
    public function get()
128
    {
129
        return $this->tweets;
130
    }
131
132
    /**
133
     * @param callable $filter
134
     *
135
     * @return TweetCollection
136
     */
137
    public function filter(callable $filter)
138
    {
139
        $tweets = array_filter($this->tweets, $filter);
140
141
        return new self(array_values($tweets));
142
    }
143
144
    /**
145
     * @param $date
146
     *
147
     * @return \DateTime
148
     */
149
    private function formatDate($date)
150
    {
151
        return $date instanceof \DateTime ? $date : new \DateTime($date);
152
    }
153
154
}
155