Passed
Pull Request — master (#26)
by
unknown
03:14
created

Model   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 139
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCollection() 0 4 1
A withCollection() 0 7 1
A filterStringInstance() 0 6 2
A filterUriInstance() 0 12 3
A filterDateInstance() 0 12 4
A formatDate() 0 4 1
A createLeadFromBody() 0 19 1
A __call() 0 8 2
1
<?php
2
3
namespace One\Model;
4
5
use One\Collection;
6
use Psr\Http\Message\UriInterface;
7
8
/**
9
 * Model base class
10
 * @method \One\Collection getCollection()
11
 * @method self withCollection(Collection $collection)
12
 * @method mixed|null get(string $key)
13
 * @method self set(string $key, mixed $value)
14
 * @method self add(string $key, mixed $value)
15
 * @method self map(\Closure $callback, array $context)
16
 * @method self filter filter(\Closure $callback)
17
 */
18
class Model
19
{
20
    const LEAD_SENTENCES_LIMIT = 5;
21
22
    /**
23
     * data variable to that work as One\Collection
24
     *
25
     * @var \One\Collection
26
     */
27
    protected $collection = null;
28
29
    /**
30
     * get Data Collection
31
     *
32
     * @return \One\Collection
33
     */
34
    public function getCollection()
35
    {
36
        return $this->collection;
37
    }
38
39
    /**
40
     * immutable, return CLONE of original object with changed collection
41
     *
42
     * @param \One\Collection $collection
43
     * @return self
44
     */
45
    public function withCollection(Collection $collection)
46
    {
47
        $clone = clone $this;
48
        $clone->collection = $collection;
49
50
        return $clone;
51
    }
52
53
    /**
54
     * Clean non parseable char from string
55
     *
56
     * @param $string
57
     * @return string
58
     */
59
    protected function filterStringInstance($string)
60
    {
61
        if (!empty($string)) {
62
            return htmlentities($string);
63
        }
64
    }
65
66
    /**
67
     * Make Sure Uri is a Psr\Http\Message\UriInterface instance
68
     *
69
     * @param \Psr\Http\Message\UriInterface|string|null $uri
70
     * @return string
71
     */
72
    protected function filterUriInstance($uri)
73
    {
74
        if ($uri instanceof UriInterface) {
75
            return (string) $uri;
76
        }
77
78
        if (is_string($uri)) {
79
            return (string) \One\createUriFromString($uri);
80
        }
81
82
        return (string) \One\createuriFromServer();
83
    }
84
85
    /**
86
     * Make Sure Date in string with correct format state
87
     *
88
     * @param \DateTimeInterface|string|int|null $date
89
     * @return string
90
     */
91
    protected function filterDateInstance($date)
92
    {
93
        if (empty($date)) {
94
            $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta"));
95
        }
96
97
        if (is_string($date) || is_int($date)) {
98
            $date = new \DateTime($date, new \DateTimeZone("Asia/Jakarta"));
99
        }
100
101
        return $this->formatDate($date);
102
    }
103
104
    /**
105
     * format date into required format
106
     *
107
     * @param \DateTimeInterface $date
108
     * @return string
109
     */
110
    protected function formatDate($date)
111
    {
112
        return $date->format("Y-m-d H:i:s");
113
    }
114
115
    /**
116
     * create lead/synopsis from body content if not available
117
     *
118
     * @param string $body
119
     * @return string
120
     */
121
    protected function createLeadFromBody($body)
122
    {
123
        $body = strip_tags($body);
124
        $sentences = array_filter(
125
            explode(".", $body),
126
            function ($item) {
127
                return trim($item);
128
            }
129
        );
130
131
        return implode(
132
            '. ',
133
            array_slice(
134
                $sentences,
135
                0,
136
                self::LEAD_SENTENCES_LIMIT
137
            )
138
        );
139
    }
140
141
    /**
142
     * proxy method to chain it to Collection class
143
     *
144
     * @param string $name
145
     * @param array $arguments
146
     * @throws \Exception
147
     */
148
    public function __call($name, $arguments)
149
    {
150
        if (method_exists($this->getCollection(), $name)) {
151
            return call_user_func_array(array($this->getCollection(), $name), $arguments);
152
        }
153
154
        throw new \Exception("method $name not exist");
155
    }
156
}
157