Refactor
This commit is contained in:
1 parent
0c48352677
commit
a34a951b97
16 files changed
+249
-892
No files matched your search
Executable
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Mattitja\Cision;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Http\Client\Response;
|
||||
|
||||
class Cision
|
||||
{
|
||||
protected string $slug;
|
||||
|
||||
public function __construct(string $slug = null)
|
||||
{
|
||||
$this->slug = $slug ?? env('LARAVEL_CISION_FEED_SLUG', '');
|
||||
}
|
||||
|
||||
public function all(int $page = 1): array|false
|
||||
{
|
||||
return $this->fetch($page);
|
||||
}
|
||||
|
||||
public function press(int $page = 1): array|false
|
||||
{
|
||||
return $this->fetch($page, 'Press');
|
||||
}
|
||||
|
||||
public function financial(int $page = 1): array|false
|
||||
{
|
||||
return $this->fetch($page, 'Financial');
|
||||
}
|
||||
|
||||
public function news(int $page = 1): array|false
|
||||
{
|
||||
return $this->fetch($page, 'News');
|
||||
}
|
||||
|
||||
public function media(int $page = 1): array|false
|
||||
{
|
||||
return $this->fetch($page, 'Media');
|
||||
}
|
||||
|
||||
protected function fetch(int $page = 1, ?string $type = null): array|false
|
||||
{
|
||||
$response = $this->makeRequest($page, $type);
|
||||
|
||||
if ($response === false || $response->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->parseResponse($response->body(), $page);
|
||||
}
|
||||
|
||||
protected function makeRequest(int $page, ?string $type = null): Response|false
|
||||
{
|
||||
if (empty($this->slug)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = [
|
||||
'n' => $this->slug,
|
||||
'pageIx' => $page,
|
||||
'format' => 'rss',
|
||||
];
|
||||
|
||||
if ($type) {
|
||||
$query['m'] = $type;
|
||||
}
|
||||
|
||||
return Http::get('https://news.cision.com/se/ListItems?' . http_build_query($query));
|
||||
}
|
||||
|
||||
protected function parseResponse(string $data, int $page): array|false
|
||||
{
|
||||
if (empty($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
|
||||
if ($xml === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$json = json_encode($xml);
|
||||
$rows = json_decode($json, true);
|
||||
|
||||
if (!isset($rows['channel']['item'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$items = array_map(fn ($item) => [
|
||||
'guid' => $item['guid'],
|
||||
'title' => $item['title'],
|
||||
'description' => $item['description'],
|
||||
'link' => $item['link'],
|
||||
'created_at' => Carbon::parse($item['pubDate'])->addHours(2)->format('Y-m-d H:i:s'),
|
||||
], $rows['channel']['item']);
|
||||
|
||||
return [
|
||||
'items' => $items,
|
||||
'previous_page' => $page > 1 ? $page - 1 : false,
|
||||
'next_page' => count($items) === 24 ? $page + 1 : false,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Mattitja\Cision;
|
||||
|
||||
use Spatie\LaravelPackageTools\Package;
|
||||
use Spatie\LaravelPackageTools\PackageServiceProvider;
|
||||
use Mattitja\Cision\Commands\CisionCommand;
|
||||
|
||||
class CisionServiceProvider extends PackageServiceProvider
|
||||
{
|
||||
public function configurePackage(Package $package): void
|
||||
{
|
||||
/*
|
||||
* This class is a Package Service Provider
|
||||
*
|
||||
* More info: https://github.com/spatie/laravel-package-tools
|
||||
*/
|
||||
$package
|
||||
->name('laravel-cision-feed');
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Mathiaspalmqvist\LaravelCisionFeed;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class LaravelCisionFeed
|
||||
{
|
||||
/**
|
||||
* @param int $page
|
||||
* @return array|false
|
||||
*/
|
||||
public static function all(int $page = 1): bool|array
|
||||
{
|
||||
$request = self::request($page);
|
||||
|
||||
if ($request->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::parse($request->body(), $page);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @return array|false
|
||||
*/
|
||||
public static function press(int $page = 1): bool|array
|
||||
{
|
||||
$request = self::request($page, 'Press');
|
||||
|
||||
if ($request->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::parse($request->body());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @return array|false
|
||||
*/
|
||||
public static function financial(int $page = 1): bool|array
|
||||
{
|
||||
$request = self::request($page, 'Financial');
|
||||
|
||||
if ($request->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::parse($request->body());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @return array|false
|
||||
*/
|
||||
public static function news(int $page = 1): bool|array
|
||||
{
|
||||
$request = self::request($page, 'News');
|
||||
|
||||
if ($request->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::parse($request->body());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @return array|false
|
||||
*/
|
||||
public static function media(int $page = 1): bool|array
|
||||
{
|
||||
$request = self::request($page, 'Media');
|
||||
|
||||
if ($request->failed()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return self::parse($request->body());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $page
|
||||
* @param string|null $m
|
||||
* @return false|\Illuminate\Http\Client\Response
|
||||
*/
|
||||
private static function request(int $page = 1, string $m = null): bool|\Illuminate\Http\Client\Response
|
||||
{
|
||||
if (env('LARAVEL_CISION_FEED_SLUG') === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$query = [
|
||||
'n' => env('LARAVEL_CISION_FEED_SLUG'),
|
||||
'pageIx' => $page,
|
||||
'format' => 'rss',
|
||||
];
|
||||
|
||||
if ($m) {
|
||||
$query['m'] = $m;
|
||||
}
|
||||
|
||||
return Http::get('https://news.cision.com/se/ListItems?'.http_build_query($query));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $data
|
||||
* @param int $page
|
||||
* @return bool|array
|
||||
*/
|
||||
private static function parse(string $data = null, int $page = 1): bool|array
|
||||
{
|
||||
if ($data === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$xml = simplexml_load_string($data, "SimpleXMLElement", LIBXML_NOCDATA);
|
||||
$json = json_encode($xml);
|
||||
$rows = json_decode($json, true);
|
||||
|
||||
if (! isset($rows['channel']['item'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$return = [];
|
||||
|
||||
foreach ($rows['channel']['item'] as $row) {
|
||||
$return['items'][] = [
|
||||
'guid' => $row['guid'],
|
||||
'title' => $row['title'],
|
||||
'description' => $row['description'],
|
||||
'link' => $row['link'],
|
||||
'created_at' => Carbon::parse($row['pubDate'])->addHours(2)->format('Y-m-d H:i:s'),
|
||||
];
|
||||
}
|
||||
|
||||
$return['previous_page'] = $page > 1 ? $page-- : false;
|
||||
$return['next_page'] = count( $rows['channel']['item'] ) === 24 ? ++$page : false;
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Mathiaspalmqvist\LaravelCisionFeed;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class LaravelCisionFeedProvider extends ServiceProvider
|
||||
{
|
||||
public function boot()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user