Skip to content

Commit

Permalink
Authors job processor.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasHermanek committed Dec 2, 2024
1 parent c9bcc9c commit 35ac0d2
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/DataFixtures/AuthorFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AnzuSystems\CoreDamBundle\Domain\Author\AuthorManager;
use AnzuSystems\CoreDamBundle\Entity\Author;
use AnzuSystems\CoreDamBundle\Entity\ExtSystem;
use AnzuSystems\CoreDamBundle\Helper\CollectionHelper;
use AnzuSystems\CoreDamBundle\Model\Enum\AuthorType;
use Doctrine\Common\Collections\ArrayCollection;
use Generator;
Expand Down Expand Up @@ -100,7 +101,7 @@ private function getData(): Generator
$childAuthor = (new Author())
->setId(self::AUTHOR_4)
->setName('AgencyA/AuthorB')
->setCurrentAuthors(new ArrayCollection([$author5, $author6]))
->setCurrentAuthors(CollectionHelper::newCollection([$author5, $author6]))
->setExtSystem($cmsExtSystem);
$childAuthor->getFlags()->setReviewed(false);

Expand Down
20 changes: 4 additions & 16 deletions src/Domain/Asset/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
namespace AnzuSystems\CoreDamBundle\Domain\Asset;

use AnzuSystems\CoreDamBundle\Domain\AbstractManager;
use AnzuSystems\CoreDamBundle\Domain\Author\AuthorProvider;
use AnzuSystems\CoreDamBundle\Entity\Asset;
use AnzuSystems\CoreDamBundle\Entity\Author;
use AnzuSystems\CoreDamBundle\Model\Dto\Asset\AssetAdmUpdateDto;
use AnzuSystems\CoreDamBundle\Model\Dto\Asset\FormProvidableMetadataBulkUpdateDto;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\NonUniqueResultException;

class AssetManager extends AbstractManager
{
public function __construct(
private readonly AssetPropertiesRefresher $propertiesRefresher
private readonly AssetPropertiesRefresher $propertiesRefresher,
private readonly AuthorProvider $authorProvider,
) {
}

Expand Down Expand Up @@ -89,20 +89,8 @@ public function updateFromMetadataBulkDto(
$this->colUpdate(
oldCollection: $asset->getAuthors(),
newCollection: $dto->getAuthors(),
addElementFn: function (Collection $oldCollection, Author $author): bool {
if (false === $author->getCurrentAuthors()->isEmpty()) {
foreach ($author->getCurrentAuthors() as $currentAuthor) {
$oldCollection->add($currentAuthor);
}

return true;
}

$oldCollection->add($author);

return true;
},
);
$this->authorProvider->provideCurrentAuthorToColl($asset);

return $this->updateExisting($asset, $flush);
}
Expand Down
1 change: 0 additions & 1 deletion src/Domain/Author/AuthorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace AnzuSystems\CoreDamBundle\Domain\Author;

use AnzuSystems\CoreDamBundle\Domain\AbstractManager;
use AnzuSystems\CoreDamBundle\Entity\AssetLicence;
use AnzuSystems\CoreDamBundle\Entity\Author;
use Doctrine\Common\Collections\Collection;

Expand Down
24 changes: 18 additions & 6 deletions src/Domain/Author/AuthorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,25 @@ public function provideByTitle(string $title, ExtSystem $extSystem): ?Author
);
}

public function provideCurrentAuthorToColl(Asset $asset, Author $author): void
public function provideCurrentAuthorToColl(Asset $asset): bool
{
$changedCurrentAuthors = false;
foreach ($asset->getAuthors() as $assetAuthor) {
if ($assetAuthor->getCurrentAuthors()->isEmpty()) {
continue;
}

}
$changedCurrentAuthors = true;

foreach ($assetAuthor->getCurrentAuthors() as $currentAuthor) {
$asset->getAuthors()->add($currentAuthor);
}

// public function ()
// {
//
// }
if (false === $assetAuthor->getFlags()->isReviewed()) {
$asset->getAuthors()->removeElement($assetAuthor);
}
}

return $changedCurrentAuthors;
}
}
93 changes: 46 additions & 47 deletions src/Domain/Job/Processor/JobAuthorCurrentOptimizeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,30 @@
use AnzuSystems\CommonBundle\Domain\Job\Processor\AbstractJobProcessor;
use AnzuSystems\CommonBundle\Entity\Interfaces\JobInterface;
use AnzuSystems\CommonBundle\Traits\EntityManagerAwareTrait;
use AnzuSystems\CoreDamBundle\Domain\Author\AuthorProvider;
use AnzuSystems\CoreDamBundle\Domain\Image\ImageCopyFacade;
use AnzuSystems\CoreDamBundle\Entity\Asset;
use AnzuSystems\CoreDamBundle\Entity\Author;
use AnzuSystems\CoreDamBundle\Entity\JobAuthorCurrentOptimize;
use AnzuSystems\CoreDamBundle\Entity\JobImageCopy;
use AnzuSystems\CoreDamBundle\Entity\JobImageCopyItem;
use AnzuSystems\CoreDamBundle\Model\Dto\Image\ImageCopyDto;
use AnzuSystems\CoreDamBundle\Model\Enum\AssetFileCopyStatus;
use AnzuSystems\CoreDamBundle\Model\ValueObject\JobImageCopyResult;
use AnzuSystems\CoreDamBundle\Model\ValueObject\JobAuthorCurrentOptimizeResult;
use AnzuSystems\CoreDamBundle\Repository\AssetRepository;
use AnzuSystems\CoreDamBundle\Repository\AuthorRepository;
use AnzuSystems\CoreDamBundle\Repository\JobImageCopyItemRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Throwable;

final class JobAuthorCurrentOptimizeProcessor extends AbstractJobProcessor
{
use EntityManagerAwareTrait;

private const int ASSET_BULK_SIZE = 10;
private const int ASSET_BULK_SIZE = 500;

public function __construct(
private readonly ImageCopyFacade $imageCopyFacade,
private readonly JobImageCopyItemRepository $jobImageCopyItemRepository,
private readonly AssetRepository $assetRepository,
private readonly AuthorRepository $authorRepository,
private readonly AuthorProvider $authorProvider,
private int $bulkSize = self::ASSET_BULK_SIZE,
) {
}
Expand All @@ -52,60 +50,61 @@ public function setBulkSize(int $bulkSize): void
*/
public function process(JobInterface $job): void
{
// $this->start($job);
$this->start($job);

try {
/** @var Collection<int, Author> $currentAuthorColl */
$authors = new ArrayCollection(array_values(
array_filter(
array_map(fn(string $id): ?Author => $this->authorRepository->find($id), $job->getAuthorIds()),
)
));

foreach ($authors as $author) {
$this->processAuthor($author);
}
$job->isProcessAll()
? $this->processAll($job)
: $this->processAuthor($job)
;
$this->entityManager->clear();
} catch (Throwable $e) {
$this->finishFail($job, $e->getMessage());
}
}

private function processAuthor(Author $author): void
private function processAll(JobAuthorCurrentOptimize $job): void
{
$assets = $this->assetRepository->findByAuthor($author);
dd($assets->count());
$lastId = $job->getLastBatchProcessedRecord();
$assets = $this->assetRepository->getAll(idFrom: $lastId, limit: $this->bulkSize);

$this->processAssetsCollection($job, $assets, $lastId);
}

private function processAuthor(JobAuthorCurrentOptimize $job): void
{
/** @var Author $author */
$author = $this->authorRepository->find($job->getAuthorId());

$lastId = $job->getLastBatchProcessedRecord();
$assets = $this->assetRepository->findByAuthor($author, $lastId, $this->bulkSize);

$this->processAssetsCollection($job, $assets, $lastId);
}

/**
* @param Collection<int, JobImageCopyItem> $items
* @param Collection<int, Asset> $assets
*/
private function finishCycle(JobImageCopy $job, Collection $items, string $lastProcessedRecord = ''): void
private function processAssetsCollection(JobAuthorCurrentOptimize $job, Collection $assets, string $lastId = ''): void
{
$previousResult = JobImageCopyResult::fromString($job->getResult());
$job->setBatchProcessedIterationCount($items->count() + $job->getBatchProcessedIterationCount());

$existsCount = $previousResult->getExistsCount();
$copyCount = $previousResult->getCopyCount();
$notAllowedCount = $previousResult->getNotAllowedCount();

foreach ($items as $item) {
match ($item->getStatus()) {
AssetFileCopyStatus::Copy => $copyCount++,
AssetFileCopyStatus::Exists => $existsCount++,
AssetFileCopyStatus::Unassigned => $notAllowedCount++,
AssetFileCopyStatus::NotAllowed => null
};
$changedAuthorsCount = 0;
foreach ($assets as $asset) {
if ($this->authorProvider->provideCurrentAuthorToColl($asset)) {
$changedAuthorsCount++;
}
$lastId = (string) $asset->getId();
}

$this->getManagedJob($job)->setResult((new JobImageCopyResult(
copyCount: $copyCount,
existsCount: $existsCount,
notAllowedCount: $notAllowedCount
))->toString());

$items->count() === $this->bulkSize
? $this->toAwaitingBatchProcess($job, $lastProcessedRecord)
: $this->finishSuccess($job)
;
$count = $assets->count();
$resultBefore = JobAuthorCurrentOptimizeResult::fromString($job->getResult());
$resultNew = new JobAuthorCurrentOptimizeResult(
$resultBefore->getOptimizedCount() + $changedAuthorsCount,
$resultBefore->getTotalCount() + $assets->count(),
);
$this->getManagedJob($job)->setResult($resultNew->toString());

$count === $this->bulkSize
? $this->toAwaitingBatchProcess($job, $lastId)
: $this->finishSuccess($job);
}
}
14 changes: 8 additions & 6 deletions src/Entity/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class Author implements UuidIdentifiableInterface, UserTrackingInterface, TimeTr
* If asset uses author and has defined currentAuthors, this relation should be replaced
* todo current author should be final author without parents!
*/
#[ORM\ManyToMany(targetEntity: Author::class, inversedBy: 'childAuthors', fetch: App::DOCTRINE_EXTRA_LAZY)]
#[ORM\ManyToMany(targetEntity: self::class, inversedBy: 'childAuthors', fetch: App::DOCTRINE_EXTRA_LAZY)]
#[ORM\JoinTable('author_is_current_author')]
#[Serialize(handler: EntityIdHandler::class)]
private Collection $currentAuthors;

/**
* Inverse side of currentAuthors
*/
#[ORM\ManyToMany(targetEntity: Author::class, mappedBy: 'currentAuthors', fetch: App::DOCTRINE_EXTRA_LAZY)]
#[ORM\ManyToMany(targetEntity: self::class, mappedBy: 'currentAuthors', fetch: App::DOCTRINE_EXTRA_LAZY)]
#[Serialize(handler: EntityIdHandler::class)]
private Collection $childAuthors;

Expand Down Expand Up @@ -164,6 +164,7 @@ public function getCurrentAuthors(): Collection
public function setCurrentAuthors(Collection $currentAuthors): self
{
$this->currentAuthors = $currentAuthors;

return $this;
}

Expand All @@ -175,7 +176,7 @@ public function getChildAuthors(): Collection
return $this->childAuthors;
}

public function addChildAuthor(Author $author): self
public function addChildAuthor(self $author): self
{
if (false === $this->childAuthors->contains($author)) {
$this->childAuthors->add($author);
Expand All @@ -184,7 +185,7 @@ public function addChildAuthor(Author $author): self
return $this;
}

public function addCurrentAuthor(Author $author): self
public function addCurrentAuthor(self $author): self
{
if (false === $this->currentAuthors->contains($author)) {
$this->currentAuthors->add($author);
Expand All @@ -199,10 +200,11 @@ public function addCurrentAuthor(Author $author): self
public function setChildAuthors(Collection $childAuthors): self
{
$this->childAuthors = $childAuthors;

return $this;
}

public function removeChildAuthor(Author $author): self
public function removeChildAuthor(self $author): self
{
if ($this->childAuthors->contains($author)) {
$this->childAuthors->removeElement($author);
Expand All @@ -211,7 +213,7 @@ public function removeChildAuthor(Author $author): self
return $this;
}

public function removeCurrentAuthor(Author $author): self
public function removeCurrentAuthor(self $author): self
{
if ($this->currentAuthors->contains($author)) {
$this->currentAuthors->removeElement($author);
Expand Down
21 changes: 10 additions & 11 deletions src/Entity/JobAuthorCurrentOptimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,20 @@

use AnzuSystems\CommonBundle\Entity\Job;
use AnzuSystems\CoreDamBundle\Repository\JobAuthorCurrentOptimizeRepository;
use AnzuSystems\CoreDamBundle\Repository\JobImageCopyRepository;
use AnzuSystems\SerializerBundle\Attributes\Serialize;
use AnzuSystems\SerializerBundle\Handler\Handlers\EntityIdHandler;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: JobAuthorCurrentOptimizeRepository::class)]
class JobAuthorCurrentOptimize extends Job
{
#[ORM\Column(type: Types::BOOLEAN)]
#[ORM\Column(type: Types::BOOLEAN, options: ['default' => false])]
#[Serialize]
private bool $processAll = false;

#[ORM\Column(type: Types::JSON)]
#[ORM\Column(type: Types::STRING, length: 36, nullable: true)]
#[Serialize]
private array $authorIds = [];
private ?string $authorId = null;

public function isProcessAll(): bool
{
Expand All @@ -32,17 +29,19 @@ public function isProcessAll(): bool
public function setProcessAll(bool $processAll): self
{
$this->processAll = $processAll;

return $this;
}

public function getAuthorIds(): array
public function getAuthorId(): ?string
{
return $this->authorIds;
return $this->authorId;
}

public function setAuthorIds(array $authorIds): self
public function setAuthorId(?string $authorId): self
{
$this->authorIds = $authorIds;
$this->authorId = $authorId;

return $this;
}
}
Loading

0 comments on commit 35ac0d2

Please sign in to comment.