Passed
Push — master ( 587c07...fe3a46 )
by Jakub
02:57
created

Generator::setManagingEditor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Rss;
5
6
/**
7
 * RSS Channel Generator
8
 *
9
 * @author Jakub Konečný
10
 * @property string $title
11
 * @property string $description
12
 * @property string $link
13
 * @property string $language
14
 * @property string $copyright
15
 * @property string $managingEditor
16
 * @property string $webMaster
17
 * @property callable $dataSource
18
 * @property int $shortenDescription
19
 * @property string $dateTimeFormat
20
 * @property callable $lastBuildDate
21
 * @property string $template
22
 * @method void onBeforeGenerate(Generator $generator)
23
 * @method void onAddItem(Generator $generator, \SimpleXMLElement $channel, RssChannelItem $itemDefinition, \SimpleXMLElement $item)
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 132 characters
Loading history...
24
 * @method void onAfterGenerate(Generator $generator)
25
 */
26 1
final class Generator {
27 1
  use \Nette\SmartObject;
28
  
29
  /** @var string */
30
  protected $title = "";
31
  /** @var string */
32
  protected $description = "";
33
  /** @var string */
34
  protected $link = "";
35
  /** @var string */
36
  protected $language = "";
37
  /** @var string */
38
  protected $copyright = "";
39
  /** @var string */
40
  protected $managingEditor = "";
41
  /** @var string */
42
  protected $webMaster = "";
43
  /** @var string */
44
  protected $dateTimeFormat = "Y-m-d H:i:s";
45
  /** @var callable|null */
46
  protected $dataSource = null;
47
  /** @var int */
48
  protected $shortenDescription = 150;
49
  /** @var callable */
50
  protected $lastBuildDate = "time";
51
  /** @var string */
52
  protected $template = __DIR__ . "/template.xml";
53
  /** @var callable[] */
54
  public $onBeforeGenerate = [];
55
  /** @var callable[] */
56
  public $onAddItem = [];
57
  /** @var callable[] */
58
  public $onAfterGenerate = [];
59
  
60
  public function getTitle(): string {
61 1
    return $this->title;
62
  }
63
  
64
  public function setTitle(string $title): void {
65 1
    $this->title = $title;
66 1
  }
67
  
68
  public function getDescription(): string {
69 1
    return $this->description;
70
  }
71
  
72
  public function setDescription(string $description): void {
73 1
    $this->description = $description;
74 1
  }
75
  
76
  public function getLink(): string {
77 1
    return $this->link;
78
  }
79
  
80
  public function setLink(string $link): void {
81 1
    $this->link = $link;
82 1
  }
83
84
  public function getLanguage(): string {
85 1
    return $this->language;
86
  }
87
88
  public function setLanguage(string $language): void {
89 1
    $this->language = $language;
90 1
  }
91
92
  public function getCopyright(): string {
93 1
    return $this->copyright;
94
  }
95
96
  public function setCopyright(string $copyright): void {
97 1
    $this->copyright = $copyright;
98 1
  }
99
100
  public function getManagingEditor(): string {
101 1
    return $this->managingEditor;
102
  }
103
104
  public function setManagingEditor(string $managingEditor): void {
105 1
    $this->managingEditor = $managingEditor;
106 1
  }
107
108
  public function getWebMaster(): string {
109 1
    return $this->webMaster;
110
  }
111
112
  public function setWebMaster(string $webMaster): void {
113 1
    $this->webMaster = $webMaster;
114 1
  }
115
116
  public function setDataSource(callable $dataSource): void {
117 1
    $this->dataSource = $dataSource;
118 1
  }
119
  
120
  public function getShortenDescription(): int {
121 1
    return $this->shortenDescription;
122
  }
123
  
124
  public function setShortenDescription(int $value): void {
125 1
    $this->shortenDescription = $value;
126 1
  }
127
  
128
  public function getDateTimeFormat(): string {
129 1
    return $this->dateTimeFormat;
130
  }
131
  
132
  public function setDateTimeFormat(string $format): void {
133 1
    $this->dateTimeFormat = $format;
134 1
  }
135
  
136
  public function getLastBuildDate(): callable {
137 1
    return $this->lastBuildDate;
138
  }
139
  
140
  public function setLastBuildDate(callable $lastBuildDate): void {
141 1
    $this->lastBuildDate = $lastBuildDate;
142 1
  }
143
  
144
  public function getTemplate(): string {
145 1
    return $this->template;
146
  }
147
  
148
  /**
149
   * @throws \RuntimeException
150
   */
151
  public function setTemplate(string $template): void {
152 1
    if(!is_file($template)) {
153 1
      throw new \RuntimeException("File $template does not exist.");
154
    }
155 1
    $this->template = $template;
156 1
  }
157
  
158
  /**
159
   * @throws InvalidStateException
160
   * @throws \InvalidArgumentException
161
   */
162
  protected function getData(): Collection {
163 1
    if(is_null($this->dataSource)) {
0 ignored issues
show
introduced by
The condition is_null($this->dataSource) is always false.
Loading history...
164 1
      throw new InvalidStateException("Data source for RSS generator is not set.");
165
    }
166 1
    $items = call_user_func($this->dataSource);
167 1
    if(!$items instanceof Collection) {
168 1
      throw new \InvalidArgumentException("Callback for data source for RSS generator has to return " . Collection::class . ".");
0 ignored issues
show
introduced by
Line exceeds 120 characters; contains 129 characters
Loading history...
169
    }
170 1
    return $items;
171
  }
172
  
173
  protected function shortenDescription(string $description): string {
174 1
    if($this->shortenDescription < 1) {
175 1
      return $description;
176
    }
177 1
    $originalDescription = $description;
178 1
    $description = substr($description, 0, $this->shortenDescription);
179 1
    if($description !== $originalDescription) {
180 1
      $description .= "...";
181
    }
182 1
    return $description;
183
  }
184
  
185
  protected function writeProperty(\SimpleXMLElement &$channel, string $property): void {
186 1
    if(isset($this->$property) AND $this->$property !== "") {
187 1
      $channel->channel->{$property}[0][0] = $this->$property;
188
    }
189 1
  }
190
  
191
  /**
192
   * @throws InvalidStateException
193
   * @throws \InvalidArgumentException
194
   */
195
  public function generate(): string {
196 1
    $this->onBeforeGenerate($this);
197 1
    $items = $this->getData();
198 1
    $lastBuildDate = call_user_func($this->lastBuildDate);
199 1
    if(!is_int($lastBuildDate)) {
200 1
      throw new \InvalidArgumentException("Callback for last build date for RSS generator has to return integer.");
201
    }
202 1
    $channel = simplexml_load_file($this->template);
203 1
    $channel->channel->lastBuildDate[0][0] = date($this->dateTimeFormat, $lastBuildDate);
204 1
    $this->writeProperty($channel, "link");
205 1
    $this->writeProperty($channel, "title");
206 1
    $this->writeProperty($channel, "description");
207 1
    $this->writeProperty($channel, "language");
208 1
    $this->writeProperty($channel, "copyright");
209 1
    $this->writeProperty($channel, "managingEditor");
210 1
    $this->writeProperty($channel, "webMaster");
211
    /** @var RssChannelItem $item */
212 1
    foreach($items as $item) {
213
      /** @var \SimpleXMLElement $i */
214 1
      $i = $channel->channel->addChild("item");
215 1
      $i->addChild("title", $item->title);
216 1
      $i->addChild("link", $item->link);
217 1
      $i->addChild("pubDate", date($this->dateTimeFormat, $item->pubDate));
218 1
      $i->addChild("description", $this->shortenDescription($item->description));
219 1
      $this->onAddItem($this, $channel, $item, $i);
220
    }
221 1
    $this->onAfterGenerate($this);
222 1
    return $channel->asXML();
223
  }
224
  
225
  /**
226
   * @throws InvalidStateException
227
   * @throws \InvalidArgumentException
228
   */
229
  public function response(): RssResponse {
230
    try {
231 1
      return new RssResponse($this->generate());
232 1
    } catch(InvalidStateException | \InvalidArgumentException $e) {
233 1
      throw $e;
234
    }
235
  }
236
}
237
?>