Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 29, 2020
0 parents commit 91d3d5d
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
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
21 changes: 21 additions & 0 deletions LICENSE
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.
4 changes: 4 additions & 0 deletions README.md
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.
2 changes: 2 additions & 0 deletions common.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extensions:
wordpressPostFeed: Baraja\WordPressPostFeed\WordpressPostFeedExtension
33 changes: 33 additions & 0 deletions composer.json
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"
}
3 changes: 3 additions & 0 deletions phpstan.neon
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
114 changes: 114 additions & 0 deletions src/Feed.php
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);
}
}
}
75 changes: 75 additions & 0 deletions src/Post.php
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;
}
}
32 changes: 32 additions & 0 deletions src/WordpressPostFeedExtension.php
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');
}
}

0 comments on commit 91d3d5d

Please sign in to comment.