Passed
Push — master ( e9f6a4...7ddfc7 )
by Jakub
01:51
created

RssChannelItem   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 157
ccs 61
cts 61
cp 1
rs 10
c 0
b 0
f 0
eloc 62
wmc 28

42 Methods

Rating   Name   Duplication   Size   Complexity  
setAuthor() 0 2 ?
getTitle() 0 2 ?
getGuid() 0 2 ?
getDescription() 0 2 ?
getCategories() 0 2 ?
A hp$1 ➔ setDescription() 0 2 1
setPubDate() 0 2 ?
A hp$1 ➔ getComments() 0 2 1
A hp$1 ➔ getEnclosures() 0 2 1
A hp$1 ➔ setTitle() 0 2 1
normalizeValue() 0 8 ?
A hp$1 ➔ getAuthor() 0 2 1
A hp$1 ➔ toXml() 0 11 4
A hp$1 ➔ getTitle() 0 2 1
shortenDescription() 0 10 ?
toXml() 0 11 ?
A hp$1 ➔ normalizeValue() 0 8 3
setGuid() 0 2 ?
A hp$1 ➔ setAuthor() 0 2 1
setTitle() 0 2 ?
A hp$1 ➔ setComments() 0 2 1
A hp$1 ➔ getDescription() 0 2 1
A hp$0 ➔ appendToXml() 0 3 1
getEnclosures() 0 2 ?
A hp$1 ➔ appendToXml() 0 3 1
A hp$1 ➔ setPubDate() 0 2 1
setComments() 0 2 ?
A hp$1 ➔ setLink() 0 2 1
A hp$1 ➔ setGuid() 0 2 1
setLink() 0 2 ?
getPubDate() 0 2 ?
A hp$1 ➔ __construct() 0 22 1
setDescription() 0 2 ?
A hp$1 ➔ shortenDescription() 0 10 3
getAuthor() 0 2 ?
A hp$1 ➔ getGuid() 0 2 1
__construct() 0 22 ?
getComments() 0 2 ?
A hp$1 ➔ getLink() 0 2 1
A hp$1 ➔ getPubDate() 0 2 1
getLink() 0 2 ?
A hp$1 ➔ getCategories() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Rss;
5
6
/**
7
 * Rss Channel Item
8
 *
9
 * @author Jakub Konečný
10
 * @property string $title
11
 * @property string $description
12
 * @property string $link
13
 * @property int $pubDate
14
 * @property string $author
15
 * @property string $comments
16
 * @property string $guid
17
 * @property \Nexendrie\Utils\Collection|Category[] $categories
18
 * @property \Nexendrie\Utils\Collection|Enclosure[] $enclosures
19
 */
20 1
class RssChannelItem {
21 1
  use \Nette\SmartObject;
22
  
23
  /** @var string */
24
  protected $title;
25
  /** @var string */
26
  protected $description;
27
  /** @var string */
28
  protected $link;
29
  /** @var int */
30
  protected $pubDate;
31
  /** @var string */
32
  protected $author = "";
33
  /** @var string */
34
  protected $comments = "";
35
  /** @var string */
36
  protected $guid = "";
37
  /** @var \Nexendrie\Utils\Collection|Category[] */
38
  protected $categories;
39
  /** @var \Nexendrie\Utils\Collection|Enclosure[] */
40
  protected $enclosures;
41
  
42
  public function __construct(string $title, string $description, string $link, int $pubDate) {
43 1
    $this->title = $title;
44 1
    $this->description = $description;
45 1
    $this->link = $link;
46 1
    $this->pubDate = $pubDate;
47 1
    $this->categories = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible {
48
      /** @var string */
49
      protected $class = Category::class;
50
51
      public function appendToXml(\SimpleXMLElement &$parent): void {
52 1
        array_walk($this->items, function(Category $value) use($parent) {
53 1
          $value->appendToXml($parent);
54 1
        });
55 1
      }
56
    };
57 1
    $this->enclosures = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible {
58
      /** @var string */
59
      protected $class = Enclosure::class;
60
61
      public function appendToXml(\SimpleXMLElement &$parent): void {
62 1
        array_walk($this->items, function(Enclosure $value) use($parent) {
63 1
          $value->appendToXml($parent);
64 1
        });
65 1
      }
66
    };
67 1
  }
68
  
69
  public function getTitle(): string {
70 1
    return $this->title;
71
  }
72
  
73
  public function setTitle(string $title): void {
74 1
    $this->title = $title;
75 1
  }
76
  
77
  public function getDescription(): string {
78 1
    return $this->description;
79
  }
80
  
81
  public function setDescription(string $description): void {
82 1
    $this->description = $description;
83 1
  }
84
  
85
  public function getLink(): string {
86 1
    return $this->link;
87
  }
88
  
89
  public function setLink(string $link): void {
90 1
    $this->link = $link;
91 1
  }
92
  
93
  public function getPubDate(): int {
94 1
    return $this->pubDate;
95
  }
96
  
97
  public function setPubDate(int $pubDate): void {
98 1
    $this->pubDate = $pubDate;
99 1
  }
100
101
  public function getAuthor(): string {
102 1
    return $this->author;
103
  }
104
105
  public function setAuthor(string $author): void {
106 1
    $this->author = $author;
107 1
  }
108
109
  public function getComments(): string {
110 1
    return $this->comments;
111
  }
112
113
  public function setComments(string $comments): void {
114 1
    $this->comments = $comments;
115 1
  }
116
117
  public function getGuid(): string {
118 1
    return $this->guid;
119
  }
120
121
  public function setGuid(string $guid): void {
122 1
    $this->guid = $guid;
123 1
  }
124
125
  /**
126
   * @return \Nexendrie\Utils\Collection|Category[]
127
   */
128
  public function getCategories(): \Nexendrie\Utils\Collection {
129 1
    return $this->categories;
130
  }
131
132
  /**
133
   * @return \Nexendrie\Utils\Collection|Enclosure[]
134
   */
135
  public function getEnclosures(): \Nexendrie\Utils\Collection {
136 1
    return $this->enclosures;
137
  }
138
139
  protected function shortenDescription(string $description, int $maxLength): string {
140 1
    if($maxLength < 1) {
141 1
      return $description;
142
    }
143 1
    $originalDescription = $description;
144 1
    $description = substr($description, 0, $maxLength);
145 1
    if($description !== $originalDescription) {
146 1
      $description .= "...";
147
    }
148 1
    return $description;
149
  }
150
151
  /**
152
   * @param mixed $value
153
   * @return mixed
154
   */
155
  protected function normalizeValue(string $name, $value, Generator $generator) {
156 1
    switch($name) {
157 1
      case "pubDate":
158 1
        return date($generator->dateTimeFormat, $value);
159 1
      case "description":
160 1
        return $this->shortenDescription($value, $generator->shortenDescription);
161
      default:
162 1
        return $value;
163
    }
164
  }
165
166
  public function toXml(\SimpleXMLElement &$element, Generator $generator): void {
167 1
    $properties = array_keys(get_object_vars($this));
168 1
    foreach($properties as $property) {
169 1
      if($this->$property === "") {
170 1
        continue;
171
      }
172 1
      $value = $this->normalizeValue($property, $this->$property, $generator);
173 1
      if($value instanceof IXmlConvertible) {
174 1
        $value->appendToXml($element);
175
      } else {
176 1
        $element->addChild($property, $value);
177
      }
178
    }
179 1
  }
180
}
181
?>