Skip to content

Commit 8ee8800

Browse files
authored
Remove Link entity from pacakge read model (#473)
1 parent 0b8c242 commit 8ee8800

File tree

14 files changed

+142
-124
lines changed

14 files changed

+142
-124
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
7+
charset = utf-8
8+
max_line_length = 160
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true

src/Controller/OrganizationController.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,15 @@ public function packageDetails(Organization $organization, PackageDetails $packa
110110
{
111111
$filter = Filter::fromRequest($request);
112112

113-
$packageLinks = $this->packageQuery->getLinks($package->id(), $organization->id());
114-
115-
/** @var string $packageName */
116-
$packageName = $package->name();
117-
118-
$dependantCount = $this->packageQuery->getDependantCount($packageName, $organization->id());
119-
120-
$groupedPackageLinks = [];
121-
122-
foreach ($packageLinks as $packageLink) {
123-
if (!isset($groupedPackageLinks[$packageLink->type()])) {
124-
$groupedPackageLinks[$packageLink->type()] = [];
125-
}
126-
127-
$groupedPackageLinks[$packageLink->type()][] = $packageLink;
128-
}
129-
130113
return $this->render('organization/package/details.html.twig', [
131114
'organization' => $organization,
132115
'package' => $package,
133116
'filter' => $filter,
134117
'count' => $this->packageQuery->versionCount($package->id()),
135118
'versions' => $this->packageQuery->getVersions($package->id(), $filter),
136119
'installs' => $this->packageQuery->getInstalls($package->id(), 0),
137-
'packageLinks' => $groupedPackageLinks,
138-
'dependantCount' => $dependantCount,
120+
'packageLinks' => $this->packageQuery->getLinks($package->id(), $organization->id()),
121+
'dependantCount' => $package->name() !== null ? $this->packageQuery->getDependantCount($package->name(), $organization->id()) : 0,
139122
]);
140123
}
141124

src/Entity/Organization/Package.php

-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,6 @@ public function links(): Collection
355355

356356
public function addLink(Link $link): void
357357
{
358-
$link->setPackage($this);
359-
$link->setOrganization($this->organization);
360358
$this->links->add($link);
361359
}
362360

src/Entity/Organization/Package/Link.php

+3-35
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ class Link
2727
*/
2828
private UuidInterface $id;
2929

30-
/**
31-
* @ORM\ManyToOne(targetEntity="Buddy\Repman\Entity\Organization")
32-
* @ORM\JoinColumn(nullable=false)
33-
*/
34-
private Organization $organization;
35-
3630
/**
3731
* @ORM\ManyToOne(targetEntity="Buddy\Repman\Entity\Organization\Package", inversedBy="links")
3832
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
@@ -54,23 +48,18 @@ class Link
5448
*/
5549
private string $constraint;
5650

57-
private ?string $packageId;
58-
private ?string $targetPackageId;
59-
6051
public function __construct(
6152
UuidInterface $id,
53+
Package $package,
6254
string $type,
6355
string $target,
64-
string $constraint,
65-
?string $packageId = null,
66-
?string $targetPackageId = null
56+
string $constraint
6757
) {
6858
$this->id = $id;
59+
$this->package = $package;
6960
$this->type = $type;
7061
$this->target = $target;
7162
$this->constraint = $constraint;
72-
$this->packageId = $packageId;
73-
$this->targetPackageId = $targetPackageId;
7463
}
7564

7665
public function type(): string
@@ -87,25 +76,4 @@ public function constraint(): string
8776
{
8877
return $this->constraint;
8978
}
90-
91-
public function targetPackageId(): ?string
92-
{
93-
return $this->targetPackageId;
94-
}
95-
96-
public function setOrganization(Organization $organization): void
97-
{
98-
if (isset($this->organization)) {
99-
throw new \RuntimeException('You can not change link organization');
100-
}
101-
$this->organization = $organization;
102-
}
103-
104-
public function setPackage(Package $package): void
105-
{
106-
if (isset($this->package)) {
107-
throw new \RuntimeException('You can not change link package');
108-
}
109-
$this->package = $package;
110-
}
11179
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Buddy\Repman\Migrations;
6+
7+
use Doctrine\DBAL\Schema\Schema;
8+
use Doctrine\Migrations\AbstractMigration;
9+
10+
/**
11+
* Auto-generated Migration: Please modify to your needs!
12+
*/
13+
final class Version20210531095502 extends AbstractMigration
14+
{
15+
public function getDescription(): string
16+
{
17+
return '';
18+
}
19+
20+
public function up(Schema $schema): void
21+
{
22+
// this up() migration is auto-generated, please modify it to your needs
23+
$this->addSql('ALTER TABLE organization_package_link DROP CONSTRAINT fk_4a06082932c8a3de');
24+
$this->addSql('DROP INDEX idx_4a06082932c8a3de');
25+
$this->addSql('ALTER TABLE organization_package_link DROP organization_id');
26+
}
27+
28+
public function down(Schema $schema): void
29+
{
30+
// this down() migration is auto-generated, please modify it to your needs
31+
$this->addSql('ALTER TABLE organization_package_link ADD organization_id UUID NOT NULL');
32+
$this->addSql('COMMENT ON COLUMN organization_package_link.organization_id IS \'(DC2Type:uuid)\'');
33+
$this->addSql('ALTER TABLE organization_package_link ADD CONSTRAINT fk_4a06082932c8a3de FOREIGN KEY (organization_id) REFERENCES organization (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
34+
$this->addSql('CREATE INDEX idx_4a06082932c8a3de ON organization_package_link (organization_id)');
35+
}
36+
}

src/Query/User/Model/Package/Link.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Buddy\Repman\Query\User\Model\Package;
6+
7+
final class Link
8+
{
9+
private string $type;
10+
private string $target;
11+
private string $constraint;
12+
private ?string $targetPackageId;
13+
14+
public function __construct(string $type, string $target, string $constraint, ?string $targetPackageId = null)
15+
{
16+
$this->type = $type;
17+
$this->target = $target;
18+
$this->constraint = $constraint;
19+
$this->targetPackageId = $targetPackageId;
20+
}
21+
22+
public function type(): string
23+
{
24+
return $this->type;
25+
}
26+
27+
public function target(): string
28+
{
29+
return $this->target;
30+
}
31+
32+
public function constraint(): string
33+
{
34+
return $this->constraint;
35+
}
36+
37+
public function targetPackageId(): ?string
38+
{
39+
return $this->targetPackageId;
40+
}
41+
}

src/Query/User/PackageQuery.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Buddy\Repman\Query\User;
66

7-
use Buddy\Repman\Entity\Organization\Package\Link;
87
use Buddy\Repman\Query\Filter;
98
use Buddy\Repman\Query\User\Model\Installs;
109
use Buddy\Repman\Query\User\Model\Package;
10+
use Buddy\Repman\Query\User\Model\Package\Link;
1111
use Buddy\Repman\Query\User\Model\PackageDetails;
1212
use Buddy\Repman\Query\User\Model\PackageName;
1313
use Buddy\Repman\Query\User\Model\ScanResult;
@@ -48,7 +48,7 @@ public function versionCount(string $packageId): int;
4848
public function getVersions(string $packageId, Filter $filter): array;
4949

5050
/**
51-
* @return Link[]
51+
* @return array<string,Link[]>
5252
*/
5353
public function getLinks(string $packageId, string $organizationId): array;
5454

src/Query/User/PackageQuery/DbalPackageQuery.php

+21-23
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Buddy\Repman\Query\User\PackageQuery;
66

7-
use Buddy\Repman\Entity\Organization\Package\Link;
87
use Buddy\Repman\Entity\Organization\Package\Version as VersionEntity;
98
use Buddy\Repman\Query\Filter as BaseFilter;
109
use Buddy\Repman\Query\User\Model\Installs;
1110
use Buddy\Repman\Query\User\Model\Package;
11+
use Buddy\Repman\Query\User\Model\Package\Link;
1212
use Buddy\Repman\Query\User\Model\PackageDetails;
1313
use Buddy\Repman\Query\User\Model\PackageName;
1414
use Buddy\Repman\Query\User\Model\ScanResult;
@@ -17,7 +17,6 @@
1717
use Buddy\Repman\Query\User\PackageQuery;
1818
use Doctrine\DBAL\Connection;
1919
use Munus\Control\Option;
20-
use Ramsey\Uuid\Uuid;
2120

2221
final class DbalPackageQuery implements PackageQuery
2322
{
@@ -219,44 +218,43 @@ public function versionCount(string $packageId): int
219218
public function getDependantCount(string $packageName, string $organizationId): int
220219
{
221220
return (int) $this->connection->fetchOne(
222-
'SELECT
223-
COUNT(DISTINCT package_id)
224-
FROM organization_package_link
225-
WHERE target = :package_name
226-
AND organization_id = :organization_id', [
221+
'SELECT COUNT (DISTINCT l.package_id)
222+
FROM organization_package_link l
223+
JOIN organization_package p ON p.id = l.package_id
224+
WHERE l.target = :package_name
225+
AND p.organization_id = :organization_id', [
227226
':package_name' => $packageName,
228227
':organization_id' => $organizationId,
229228
]);
230229
}
231230

232231
/**
233-
* @return Link[]
232+
* @return array<string,Link[]>
234233
*/
235234
public function getLinks(string $packageId, string $organizationId): array
236235
{
237-
return array_map(function (array $data): Link {
238-
return new Link(
239-
Uuid::fromString($data['id']),
240-
$data['type'],
241-
$data['target'],
242-
$data['constraint'],
243-
$data['package_id'],
244-
$data['target_package_id']
245-
);
246-
}, $this->connection->fetchAllAssociative(
236+
$links = [];
237+
foreach ($this->connection->fetchAllAssociative(
247238
'SELECT
248-
l.id,
249239
l.type,
250240
l.target,
251241
l.constraint,
252-
l.package_id,
253242
p.id as target_package_id
254243
FROM organization_package_link l
255244
LEFT JOIN organization_package p ON (p.name = l.target AND p.organization_id = :organization_id)
256245
WHERE package_id = :package_id', [
257-
':organization_id' => $organizationId,
258-
':package_id' => $packageId,
259-
]));
246+
':organization_id' => $organizationId,
247+
':package_id' => $packageId,
248+
]) as $data) {
249+
$links[(string) $data['type']][] = new Link(
250+
$data['type'],
251+
$data['target'],
252+
$data['constraint'],
253+
$data['target_package_id']
254+
);
255+
}
256+
257+
return $links;
260258
}
261259

262260
/**

src/Service/PackageSynchronizer/ComposerPackageSynchronizer.php

+7-24
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Buddy\Repman\Service\PackageSynchronizer;
66

77
use Buddy\Repman\Entity\Organization\Package;
8+
use Buddy\Repman\Entity\Organization\Package\Link;
89
use Buddy\Repman\Entity\Organization\Package\Version;
910
use Buddy\Repman\Repository\PackageRepository;
1011
use Buddy\Repman\Service\Dist;
@@ -19,7 +20,7 @@
1920
use Composer\IO\BufferIO;
2021
use Composer\IO\IOInterface;
2122
use Composer\Package\CompletePackage;
22-
use Composer\Package\Link;
23+
use Composer\Package\Link as ComposerLink;
2324
use Composer\Package\PackageInterface;
2425
use Composer\Repository\RepositoryFactory;
2526
use Composer\Repository\RepositoryInterface;
@@ -158,38 +159,20 @@ public function synchronize(Package $package): void
158159
$this->readmeExtractor->extractReadme($package, $dist);
159160

160161
// Set the version links
161-
$types = ['requires', 'devRequires', 'provides', 'replaces', 'conflicts'];
162-
163-
foreach ($types as $type) {
164-
/** @var Link[] $links */
162+
foreach (['requires', 'devRequires', 'provides', 'replaces', 'conflicts'] as $type) {
165163
$functionName = 'get'.$type;
166164
if (method_exists($latest, $functionName)) {
167-
$links = $latest->{$functionName}();
168-
169-
foreach ($links as $link) {
170-
$package->addLink(
171-
new Package\Link(
172-
Uuid::uuid4(),
173-
$type,
174-
$link->getTarget(),
175-
$link->getPrettyConstraint(),
176-
)
177-
);
165+
/** @var ComposerLink $link */
166+
foreach ($latest->{$functionName}() as $link) {
167+
$package->addLink(new Link(Uuid::uuid4(), $package, $type, $link->getTarget(), $link->getPrettyConstraint()));
178168
$encounteredLinks[$type.'-'.$link->getTarget()] = true;
179169
}
180170
}
181171
}
182172

183173
// suggests are different
184174
foreach ($latest->getSuggests() as $linkName => $linkDescription) {
185-
$package->addLink(
186-
new Package\Link(
187-
Uuid::uuid4(),
188-
'suggests',
189-
$linkName,
190-
$linkDescription,
191-
)
192-
);
175+
$package->addLink(new Link(Uuid::uuid4(), $package, 'suggests', $linkName, $linkDescription));
193176
$encounteredLinks['suggests-'.$linkName] = true;
194177
}
195178
}

0 commit comments

Comments
 (0)