-
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
1 parent
8daa166
commit cbcae7b
Showing
60 changed files
with
1,898 additions
and
102 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
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
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
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Factories; | ||
|
||
use App\Models\Article as ArticleModel; | ||
use Domain\Entity\Article; | ||
use Domain\Entity\Category; | ||
use Domain\Entity\Source; | ||
use Domain\Exception\DomainException; | ||
use Domain\ValueObject\Url; | ||
|
||
class ArticleFactory | ||
{ | ||
/** | ||
* @throws DomainException | ||
*/ | ||
public static function fromEloquentModel(ArticleModel $model): Article | ||
{ | ||
return new Article( | ||
id: $model->getId(), | ||
title: $model->title, | ||
description: $model->description, | ||
content: $model->content, | ||
url: new Url($model->url), | ||
imageUrl: $model->image_url !== null ? new Url($model->image_url) : null, | ||
author: $model->author !== null ? AuthorFactory::fromEloquentModel($model->author) : null, | ||
source: new Source( | ||
id: $model->source->getId(), | ||
code: $model->source->code, | ||
name: $model->source->name, | ||
), | ||
category: new Category( | ||
id: $model->category->getId(), | ||
code: $model->category->code, | ||
name: $model->category->name, | ||
), | ||
publishedAt: $model->published_at->toDateTimeImmutable(), | ||
providerCode: $model->provider_code, | ||
); | ||
} | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Factories; | ||
|
||
use App\Models\Author as AuthorEloquentModel; | ||
use Domain\Entity\Author; | ||
use Domain\Exception\DomainException; | ||
use Domain\ValueObject\FullName; | ||
|
||
class AuthorFactory | ||
{ | ||
/** | ||
* @throws DomainException | ||
*/ | ||
public static function fromEloquentModel(AuthorEloquentModel $author): Author | ||
{ | ||
return new Author( | ||
id: $author->getId(), | ||
fullName: new FullName( | ||
firstName: $author->first_name, | ||
lastName: $author->last_name, | ||
), | ||
); | ||
} | ||
} |
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
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Services\ArticleProviders\NewsApi\CategoryFinder; | ||
use App\Services\ArticleProviders\NewsApi\Dto\ArticleResponseDto; | ||
use App\Services\ArticleProviders\NewsApi\SourceFinder; | ||
use DateTimeImmutable; | ||
use DateTimeInterface; | ||
use Domain\Entity\Article; | ||
use Domain\Entity\Author; | ||
use Domain\Entity\Category; | ||
use Domain\Entity\Source; | ||
use Domain\Enum\ArticleProviderCode; | ||
use Domain\Exception\DomainException; | ||
use Domain\Exception\ExternalException; | ||
use Domain\Repository\ArticleRepositoryInterface; | ||
use Domain\Repository\AuthorRepositoryInterface; | ||
use Domain\ValueObject\FullName; | ||
use Domain\ValueObject\Url; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
class SaveNewsApiArticleJob implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
private AuthorRepositoryInterface $authorRepository; | ||
private SourceFinder $sourceFinder; | ||
private CategoryFinder $categoryFinder; | ||
|
||
public function __construct( | ||
public readonly ArticleResponseDto $articleResponseDto, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws DomainException | ||
* @throws ExternalException | ||
*/ | ||
public function handle( | ||
ArticleRepositoryInterface $articleRepository, | ||
AuthorRepositoryInterface $authorRepository, | ||
SourceFinder $sourceFinder, | ||
CategoryFinder $categoryFinder, | ||
): void { | ||
$articleRepository1 = $articleRepository; | ||
$this->authorRepository = $authorRepository; | ||
$this->sourceFinder = $sourceFinder; | ||
$this->categoryFinder = $categoryFinder; | ||
|
||
$source = $this->getSource(); | ||
$article = new Article( | ||
id: Uuid::uuid4(), | ||
title: $this->articleResponseDto->title, | ||
description: $this->articleResponseDto->description, | ||
content: $this->articleResponseDto->content, | ||
url: new Url($this->articleResponseDto->url), | ||
imageUrl: $this->articleResponseDto->urlToImage !== null | ||
? new Url($this->articleResponseDto->urlToImage) | ||
: null, | ||
author: $this->getAuthor(), | ||
source: $source, | ||
category: $this->getCategory($source), | ||
publishedAt: DateTimeImmutable::createFromFormat( | ||
DateTimeInterface::RFC3339, | ||
$this->articleResponseDto->publishedAt, | ||
), | ||
providerCode: ArticleProviderCode::NEWS_API, | ||
); | ||
|
||
$articleRepository1->save($article); | ||
} | ||
|
||
/** | ||
* @throws DomainException | ||
*/ | ||
private function getAuthor(): ?Author | ||
{ | ||
if ($this->articleResponseDto->author === null) { | ||
return null; | ||
} | ||
|
||
return $this->authorRepository->findOrCreateByFullName( | ||
FullName::fromString($this->articleResponseDto->author), | ||
); | ||
} | ||
|
||
/** | ||
* @throws ExternalException | ||
*/ | ||
private function getSource(): Source | ||
{ | ||
return $this->sourceFinder->findSource($this->articleResponseDto->source->id); | ||
} | ||
|
||
/** | ||
* @throws ExternalException | ||
*/ | ||
private function getCategory(Source $source): Category | ||
{ | ||
return $this->categoryFinder->findBySource($source); | ||
} | ||
} |
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Domain\Enum\ArticleProviderCode; | ||
use Illuminate\Database\Eloquent\Concerns\HasTimestamps; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Support\Carbon; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
/** | ||
* @property string $id | ||
* @property string $title | ||
* @property string $description | ||
* @property string $content | ||
* @property string $url | ||
* @property string|null $image_url | ||
* @property Carbon $published_at | ||
* @property Source $source | ||
* @property Author|null $author | ||
* @property Category $category | ||
* @property ArticleProviderCode $provider_code | ||
*/ | ||
class Article extends Model | ||
{ | ||
use HasTimestamps; | ||
use HasUuids; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'title', | ||
'description', | ||
'content', | ||
'url', | ||
'image_url', | ||
'author_id', | ||
'source_id', | ||
'category_id', | ||
'published_at', | ||
'provider_code', | ||
]; | ||
|
||
protected $casts = [ | ||
'published_at' => 'datetime', | ||
'provider_code' => ArticleProviderCode::class, | ||
]; | ||
|
||
public function getId(): UuidInterface | ||
{ | ||
return $this->id instanceof UuidInterface ? $this->id : Uuid::fromString($this->id); | ||
} | ||
|
||
public function source(): BelongsTo | ||
{ | ||
return $this->belongsTo(Source::class); | ||
} | ||
|
||
public function author(): BelongsTo | ||
{ | ||
return $this->belongsTo(Author::class); | ||
} | ||
|
||
public function category(): BelongsTo | ||
{ | ||
return $this->belongsTo(Category::class); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasTimestamps; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
/** | ||
* @property string $id | ||
* @property string $first_name | ||
* @property string $last_name | ||
*/ | ||
class Author extends Model | ||
{ | ||
use HasTimestamps; | ||
use HasUuids; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'first_name', | ||
'last_name', | ||
]; | ||
|
||
public static function createFromEntity(\Domain\Entity\Author $author): self | ||
{ | ||
return new self([ | ||
'first_name' => $author->fullName->firstName, | ||
'last_name' => $author->fullName->lastName, | ||
]); | ||
} | ||
|
||
public function getId(): UuidInterface | ||
{ | ||
return $this->id instanceof UuidInterface ? $this->id : Uuid::fromString($this->id); | ||
} | ||
} |
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 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Concerns\HasTimestamps; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Ramsey\Uuid\Uuid; | ||
use Ramsey\Uuid\UuidInterface; | ||
|
||
/** | ||
* @property string $id | ||
* @property string $code | ||
* @property string $name | ||
*/ | ||
class Category extends Model | ||
{ | ||
use HasTimestamps; | ||
use HasUuids; | ||
|
||
protected $fillable = [ | ||
'id', | ||
'code', | ||
'name', | ||
]; | ||
|
||
public function getId(): UuidInterface | ||
{ | ||
return $this->id instanceof UuidInterface ? $this->id : Uuid::fromString($this->id); | ||
} | ||
} |
Oops, something went wrong.