-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 91d3d5d
Showing
9 changed files
with
316 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Integrity check | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@master | ||
|
||
- name: Install PHP | ||
uses: shivammathur/setup-php@master | ||
with: | ||
php-version: 7.4 | ||
|
||
- name: Install composer deps | ||
run: | | ||
composer create-project nette/code-checker temp/code-checker ^3 --no-progress | ||
composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress | ||
# Install app deps | ||
composer install --no-interaction --prefer-dist | ||
# Check code checker and coding standards | ||
- name: Check coding standards | ||
run: | | ||
php temp/code-checker/code-checker --short-arrays --strict-types --fix --no-progress | ||
php temp/coding-standard/ecs check src --config temp/coding-standard/coding-standard-php71.yml | ||
- name: Check PHPStan rules | ||
run: composer phpstan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Baraja packages | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Wordpress post feed | ||
=================== | ||
|
||
API service for downloading a list of new blog posts. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
extensions: | ||
wordpressPostFeed: Baraja\WordPressPostFeed\WordpressPostFeedExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "baraja-core/wordpress-post-feed", | ||
"description": "API service for downloading a list of new blog posts.", | ||
"homepage": "https://github.com/baraja-core/wordpress-post-feed", | ||
"authors": [ | ||
{ | ||
"name": "Jan Barášek", | ||
"homepage": "https://baraja.cz" | ||
} | ||
], | ||
"require": { | ||
"php": ">=7.4.0", | ||
"nette/di": "^3.0", | ||
"nette/caching": "^3.0" | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^0.12.18", | ||
"tracy/tracy": "^2.7", | ||
"phpstan/phpstan-nette": "^0.12.6", | ||
"symplify/easy-coding-standard": "^7.2" | ||
}, | ||
"autoload": { | ||
"classmap": [ | ||
"src/" | ||
] | ||
}, | ||
"scripts": { | ||
"phpstan": [ | ||
"vendor/bin/phpstan analyse src -c phpstan.neon --level 6 --no-progress" | ||
] | ||
}, | ||
"minimum-stability": "stable" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
includes: | ||
- vendor/phpstan/phpstan-nette/extension.neon | ||
- vendor/phpstan/phpstan-nette/rules.neon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\WordPressPostFeed; | ||
|
||
|
||
use Nette\Caching\Cache; | ||
use Nette\Caching\IStorage; | ||
|
||
final class Feed | ||
{ | ||
private Cache $cache; | ||
|
||
private string $expirationTime; | ||
|
||
|
||
public function __construct(IStorage $storage, string $expirationTime) | ||
{ | ||
$this->cache = new Cache($storage, 'wordpress-post-feed'); | ||
$this->expirationTime = $expirationTime; | ||
} | ||
|
||
|
||
/** | ||
* @return Post[] | ||
*/ | ||
public function load(string $url): array | ||
{ | ||
$rss = new \DOMDocument(); | ||
$rss->loadXML($this->getContent($url)); | ||
|
||
$feed = []; | ||
foreach ($rss->getElementsByTagName('item') as $node) { | ||
/** @var \DOMElement $node */ | ||
$feed[] = new Post( | ||
strip_tags($this->hydrateValueToString($node, 'title')), | ||
html_entity_decode(preg_replace('/^<p>(.+?)<\/p>.*/', '$1', str_replace("\n", ' ', $this->hydrateValueToString($node, 'description')))), | ||
$this->hydrateValueToString($node, 'link'), | ||
$this->hydrateValueToDateTime($node, 'pubDate'), | ||
$this->hydrateValueToString($node, 'creator'), | ||
$this->hydrateValue($node, 'category') | ||
); | ||
} | ||
|
||
return $feed; | ||
} | ||
|
||
|
||
public function updateCache(string $url): void | ||
{ | ||
$this->getContent($url, true); | ||
} | ||
|
||
|
||
public function clearCache(): void | ||
{ | ||
$this->cache->clean([Cache::ALL => true]); | ||
} | ||
|
||
|
||
private function getContent(string $url, bool $flush = false): string | ||
{ | ||
if ($flush === true || ($cache = $this->cache->load($url)) === null) { | ||
$curl = curl_init(); | ||
curl_setopt_array($curl, [ | ||
CURLOPT_URL => $url, | ||
CURLOPT_RETURNTRANSFER => true, | ||
CURLOPT_ENCODING => 'UTF-8', | ||
]); | ||
$cache = curl_exec($curl); | ||
curl_close($curl); | ||
$this->cache->save($url, $cache, [ | ||
Cache::EXPIRATION => $this->expirationTime, | ||
]); | ||
} | ||
|
||
return $cache; | ||
} | ||
|
||
|
||
/** | ||
* @return string[] | ||
*/ | ||
private function hydrateValue(\DOMElement $node, string $key): array | ||
{ | ||
$return = []; | ||
for ($i = 0; true; $i++) { | ||
if (($item = $node->getElementsByTagName($key)->item($i)) !== null) { | ||
$return[] = $item->nodeValue; | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
|
||
private function hydrateValueToString(\DOMElement $node, string $key): ?string | ||
{ | ||
return $this->hydrateValue($node, $key)[0] ?? null; | ||
} | ||
|
||
|
||
private function hydrateValueToDateTime(\DOMElement $node, string $key): \DateTimeImmutable | ||
{ | ||
try { | ||
return new \DateTimeImmutable($this->hydrateValueToString($node, $key) ?? 'now'); | ||
} catch (\Throwable $e) { | ||
throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\WordPressPostFeed; | ||
|
||
|
||
class Post | ||
{ | ||
private string $title; | ||
|
||
private string $description; | ||
|
||
private string $link; | ||
|
||
private \DateTimeImmutable $date; | ||
|
||
private string $creator; | ||
|
||
/** @var string[] */ | ||
private array $categories; | ||
|
||
|
||
/** | ||
* @param string[] $categories | ||
*/ | ||
public function __construct(string $title, string $description, string $link, \DateTimeImmutable $date, string $creator, array $categories) | ||
{ | ||
$this->title = $title; | ||
$this->description = $description; | ||
$this->link = $link; | ||
$this->date = $date; | ||
$this->creator = $creator; | ||
$this->categories = $categories; | ||
} | ||
|
||
|
||
public function getTitle(): string | ||
{ | ||
return $this->title; | ||
} | ||
|
||
|
||
public function getDescription(): string | ||
{ | ||
return $this->description; | ||
} | ||
|
||
|
||
public function getLink(): string | ||
{ | ||
return $this->link; | ||
} | ||
|
||
|
||
public function getDate(): \DateTimeImmutable | ||
{ | ||
return $this->date; | ||
} | ||
|
||
|
||
public function getCreator(): string | ||
{ | ||
return $this->creator; | ||
} | ||
|
||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getCategories(): array | ||
{ | ||
return $this->categories; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Baraja\WordPressPostFeed; | ||
|
||
|
||
use Nette\DI\CompilerExtension; | ||
use Nette\Schema\Expect; | ||
use Nette\Schema\Schema; | ||
|
||
final class WordpressPostFeedExtension extends CompilerExtension | ||
{ | ||
public function getConfigSchema(): Schema | ||
{ | ||
return Expect::structure([ | ||
'expirationTime' => Expect::string('2 hours'), | ||
])->castTo('array'); | ||
} | ||
|
||
|
||
public function beforeCompile(): void | ||
{ | ||
$builder = $this->getContainerBuilder(); | ||
/** @var mixed[] $config */ | ||
$config = $this->getConfig(); | ||
|
||
$builder->addDefinition($this->prefix('feed')) | ||
->setFactory(Feed::class) | ||
->setArgument('expirationTime', $config['expirationTime'] ?? '2 hours'); | ||
} | ||
} |