Skip to content

Commit

Permalink
Remove Link entity from pacakge read model (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas authored May 31, 2021
1 parent 0b8c242 commit 8ee8800
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 124 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
indent_style = space
indent_size = 4

charset = utf-8
max_line_length = 160
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
21 changes: 2 additions & 19 deletions src/Controller/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,32 +110,15 @@ public function packageDetails(Organization $organization, PackageDetails $packa
{
$filter = Filter::fromRequest($request);

$packageLinks = $this->packageQuery->getLinks($package->id(), $organization->id());

/** @var string $packageName */
$packageName = $package->name();

$dependantCount = $this->packageQuery->getDependantCount($packageName, $organization->id());

$groupedPackageLinks = [];

foreach ($packageLinks as $packageLink) {
if (!isset($groupedPackageLinks[$packageLink->type()])) {
$groupedPackageLinks[$packageLink->type()] = [];
}

$groupedPackageLinks[$packageLink->type()][] = $packageLink;
}

return $this->render('organization/package/details.html.twig', [
'organization' => $organization,
'package' => $package,
'filter' => $filter,
'count' => $this->packageQuery->versionCount($package->id()),
'versions' => $this->packageQuery->getVersions($package->id(), $filter),
'installs' => $this->packageQuery->getInstalls($package->id(), 0),
'packageLinks' => $groupedPackageLinks,
'dependantCount' => $dependantCount,
'packageLinks' => $this->packageQuery->getLinks($package->id(), $organization->id()),
'dependantCount' => $package->name() !== null ? $this->packageQuery->getDependantCount($package->name(), $organization->id()) : 0,
]);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Entity/Organization/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,6 @@ public function links(): Collection

public function addLink(Link $link): void
{
$link->setPackage($this);
$link->setOrganization($this->organization);
$this->links->add($link);
}

Expand Down
38 changes: 3 additions & 35 deletions src/Entity/Organization/Package/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ class Link
*/
private UuidInterface $id;

/**
* @ORM\ManyToOne(targetEntity="Buddy\Repman\Entity\Organization")
* @ORM\JoinColumn(nullable=false)
*/
private Organization $organization;

/**
* @ORM\ManyToOne(targetEntity="Buddy\Repman\Entity\Organization\Package", inversedBy="links")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
Expand All @@ -54,23 +48,18 @@ class Link
*/
private string $constraint;

private ?string $packageId;
private ?string $targetPackageId;

public function __construct(
UuidInterface $id,
Package $package,
string $type,
string $target,
string $constraint,
?string $packageId = null,
?string $targetPackageId = null
string $constraint
) {
$this->id = $id;
$this->package = $package;
$this->type = $type;
$this->target = $target;
$this->constraint = $constraint;
$this->packageId = $packageId;
$this->targetPackageId = $targetPackageId;
}

public function type(): string
Expand All @@ -87,25 +76,4 @@ public function constraint(): string
{
return $this->constraint;
}

public function targetPackageId(): ?string
{
return $this->targetPackageId;
}

public function setOrganization(Organization $organization): void
{
if (isset($this->organization)) {
throw new \RuntimeException('You can not change link organization');
}
$this->organization = $organization;
}

public function setPackage(Package $package): void
{
if (isset($this->package)) {
throw new \RuntimeException('You can not change link package');
}
$this->package = $package;
}
}
36 changes: 36 additions & 0 deletions src/Migrations/Version20210531095502.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20210531095502 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE organization_package_link DROP CONSTRAINT fk_4a06082932c8a3de');
$this->addSql('DROP INDEX idx_4a06082932c8a3de');
$this->addSql('ALTER TABLE organization_package_link DROP organization_id');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE organization_package_link ADD organization_id UUID NOT NULL');
$this->addSql('COMMENT ON COLUMN organization_package_link.organization_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE organization_package_link ADD CONSTRAINT fk_4a06082932c8a3de FOREIGN KEY (organization_id) REFERENCES organization (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX idx_4a06082932c8a3de ON organization_package_link (organization_id)');
}
}
41 changes: 41 additions & 0 deletions src/Query/User/Model/Package/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Query\User\Model\Package;

final class Link
{
private string $type;
private string $target;
private string $constraint;
private ?string $targetPackageId;

public function __construct(string $type, string $target, string $constraint, ?string $targetPackageId = null)
{
$this->type = $type;
$this->target = $target;
$this->constraint = $constraint;
$this->targetPackageId = $targetPackageId;
}

public function type(): string
{
return $this->type;
}

public function target(): string
{
return $this->target;
}

public function constraint(): string
{
return $this->constraint;
}

public function targetPackageId(): ?string
{
return $this->targetPackageId;
}
}
4 changes: 2 additions & 2 deletions src/Query/User/PackageQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Buddy\Repman\Query\User;

use Buddy\Repman\Entity\Organization\Package\Link;
use Buddy\Repman\Query\Filter;
use Buddy\Repman\Query\User\Model\Installs;
use Buddy\Repman\Query\User\Model\Package;
use Buddy\Repman\Query\User\Model\Package\Link;
use Buddy\Repman\Query\User\Model\PackageDetails;
use Buddy\Repman\Query\User\Model\PackageName;
use Buddy\Repman\Query\User\Model\ScanResult;
Expand Down Expand Up @@ -48,7 +48,7 @@ public function versionCount(string $packageId): int;
public function getVersions(string $packageId, Filter $filter): array;

/**
* @return Link[]
* @return array<string,Link[]>
*/
public function getLinks(string $packageId, string $organizationId): array;

Expand Down
44 changes: 21 additions & 23 deletions src/Query/User/PackageQuery/DbalPackageQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Buddy\Repman\Query\User\PackageQuery;

use Buddy\Repman\Entity\Organization\Package\Link;
use Buddy\Repman\Entity\Organization\Package\Version as VersionEntity;
use Buddy\Repman\Query\Filter as BaseFilter;
use Buddy\Repman\Query\User\Model\Installs;
use Buddy\Repman\Query\User\Model\Package;
use Buddy\Repman\Query\User\Model\Package\Link;
use Buddy\Repman\Query\User\Model\PackageDetails;
use Buddy\Repman\Query\User\Model\PackageName;
use Buddy\Repman\Query\User\Model\ScanResult;
Expand All @@ -17,7 +17,6 @@
use Buddy\Repman\Query\User\PackageQuery;
use Doctrine\DBAL\Connection;
use Munus\Control\Option;
use Ramsey\Uuid\Uuid;

final class DbalPackageQuery implements PackageQuery
{
Expand Down Expand Up @@ -219,44 +218,43 @@ public function versionCount(string $packageId): int
public function getDependantCount(string $packageName, string $organizationId): int
{
return (int) $this->connection->fetchOne(
'SELECT
COUNT(DISTINCT package_id)
FROM organization_package_link
WHERE target = :package_name
AND organization_id = :organization_id', [
'SELECT COUNT (DISTINCT l.package_id)
FROM organization_package_link l
JOIN organization_package p ON p.id = l.package_id
WHERE l.target = :package_name
AND p.organization_id = :organization_id', [
':package_name' => $packageName,
':organization_id' => $organizationId,
]);
}

/**
* @return Link[]
* @return array<string,Link[]>
*/
public function getLinks(string $packageId, string $organizationId): array
{
return array_map(function (array $data): Link {
return new Link(
Uuid::fromString($data['id']),
$data['type'],
$data['target'],
$data['constraint'],
$data['package_id'],
$data['target_package_id']
);
}, $this->connection->fetchAllAssociative(
$links = [];
foreach ($this->connection->fetchAllAssociative(
'SELECT
l.id,
l.type,
l.target,
l.constraint,
l.package_id,
p.id as target_package_id
FROM organization_package_link l
LEFT JOIN organization_package p ON (p.name = l.target AND p.organization_id = :organization_id)
WHERE package_id = :package_id', [
':organization_id' => $organizationId,
':package_id' => $packageId,
]));
':organization_id' => $organizationId,
':package_id' => $packageId,
]) as $data) {
$links[(string) $data['type']][] = new Link(
$data['type'],
$data['target'],
$data['constraint'],
$data['target_package_id']
);
}

return $links;
}

/**
Expand Down
31 changes: 7 additions & 24 deletions src/Service/PackageSynchronizer/ComposerPackageSynchronizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Buddy\Repman\Service\PackageSynchronizer;

use Buddy\Repman\Entity\Organization\Package;
use Buddy\Repman\Entity\Organization\Package\Link;
use Buddy\Repman\Entity\Organization\Package\Version;
use Buddy\Repman\Repository\PackageRepository;
use Buddy\Repman\Service\Dist;
Expand All @@ -19,7 +20,7 @@
use Composer\IO\BufferIO;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage;
use Composer\Package\Link;
use Composer\Package\Link as ComposerLink;
use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryFactory;
use Composer\Repository\RepositoryInterface;
Expand Down Expand Up @@ -158,38 +159,20 @@ public function synchronize(Package $package): void
$this->readmeExtractor->extractReadme($package, $dist);

// Set the version links
$types = ['requires', 'devRequires', 'provides', 'replaces', 'conflicts'];

foreach ($types as $type) {
/** @var Link[] $links */
foreach (['requires', 'devRequires', 'provides', 'replaces', 'conflicts'] as $type) {
$functionName = 'get'.$type;
if (method_exists($latest, $functionName)) {
$links = $latest->{$functionName}();

foreach ($links as $link) {
$package->addLink(
new Package\Link(
Uuid::uuid4(),
$type,
$link->getTarget(),
$link->getPrettyConstraint(),
)
);
/** @var ComposerLink $link */
foreach ($latest->{$functionName}() as $link) {
$package->addLink(new Link(Uuid::uuid4(), $package, $type, $link->getTarget(), $link->getPrettyConstraint()));
$encounteredLinks[$type.'-'.$link->getTarget()] = true;
}
}
}

// suggests are different
foreach ($latest->getSuggests() as $linkName => $linkDescription) {
$package->addLink(
new Package\Link(
Uuid::uuid4(),
'suggests',
$linkName,
$linkDescription,
)
);
$package->addLink(new Link(Uuid::uuid4(), $package, 'suggests', $linkName, $linkDescription));
$encounteredLinks['suggests-'.$linkName] = true;
}
}
Expand Down
Loading

0 comments on commit 8ee8800

Please sign in to comment.