-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate PageFieldTypeGateway from the command
- Loading branch information
Showing
6 changed files
with
133 additions
and
99 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
89 changes: 89 additions & 0 deletions
89
src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeDoctrineDatabase.php
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,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MateuszBieniek\EzPlatformDatabaseHealthChecker\Persistence\Legacy\Content\Gateway; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\FetchMode; | ||
use EzSystems\EzPlatformPageFieldType\FieldType\Page\Storage\Gateway; | ||
|
||
class PageFieldTypeDoctrineDatabase implements PageFieldTypeGatewayInterface | ||
{ | ||
/** @var \Doctrine\DBAL\Connection */ | ||
public $connection; | ||
|
||
/** @var \EzSystems\EzPlatformPageFieldType\FieldType\Page\Storage\Gateway|null */ | ||
public $pageFieldTypeGateway; | ||
|
||
public function __construct(Connection $connection, ?Gateway $pageFieldTypeGateway = null) | ||
{ | ||
$this->connection = $connection; | ||
$this->pageFieldTypeGateway = $pageFieldTypeGateway; | ||
} | ||
|
||
public function countOrphanedPageRelations(): int | ||
{ | ||
$pagesQuery = $this->connection->createQueryBuilder(); | ||
$pagesQuery = $pagesQuery->select('id') | ||
->from('ezpage_pages') | ||
->getSQL(); | ||
|
||
$countQuery = $this->connection->createQueryBuilder(); | ||
$countQuery->select('COUNT(page_id)') | ||
->from('ezpage_map_zones_pages', 'p') | ||
->where( | ||
$countQuery->expr()->notIn( | ||
'page_id', | ||
$pagesQuery | ||
) | ||
); | ||
|
||
return (int) $countQuery->execute()->fetch(FetchMode::NUMERIC)[0]; | ||
} | ||
|
||
public function getOrphanedPageRelations(int $limit): array | ||
{ | ||
$pagesQuery = $this->connection->createQueryBuilder(); | ||
$pagesQuery = $pagesQuery->select('id') | ||
->from('ezpage_pages') | ||
->getSQL(); | ||
|
||
$orphanedPagesQuery = $this->connection->createQueryBuilder(); | ||
$orphanedPagesQuery->select('page_id') | ||
->from('ezpage_map_zones_pages', 'p') | ||
->where( | ||
$orphanedPagesQuery->expr()->notIn( | ||
'page_id', | ||
$pagesQuery | ||
) | ||
) | ||
->setMaxResults($limit); | ||
|
||
return $orphanedPagesQuery->execute()->fetchAll(FetchMode::COLUMN); | ||
} | ||
|
||
public function removePage(int $pageId): void | ||
{ | ||
$removedBlocks = []; | ||
$removedZones = []; | ||
|
||
foreach ($this->pageFieldTypeGateway->loadAttributesAssignedToPage($pageId) as $attribute) { | ||
$this->pageFieldTypeGateway->unassignAttributeFromBlock((int) $attribute['id'], (int) $attribute['block_id']); | ||
$this->pageFieldTypeGateway->removeAttribute((int) $attribute['id']); | ||
|
||
if (!\in_array($attribute['block_id'], $removedBlocks, true)) { | ||
$this->pageFieldTypeGateway->unassignBlockFromZone((int) $attribute['block_id'], (int) $attribute['zone_id']); | ||
$this->pageFieldTypeGateway->removeBlock((int) $attribute['block_id']); | ||
$this->pageFieldTypeGateway->removeBlockDesign((int) $attribute['block_id']); | ||
$this->pageFieldTypeGateway->removeBlockVisibility((int) $attribute['block_id']); | ||
$removedBlocks[] = $attribute['block_id']; | ||
} | ||
|
||
if (!\in_array($attribute['zone_id'], $removedZones, true)) { | ||
$this->pageFieldTypeGateway->unassignZoneFromPage((int) $attribute['zone_id'], $pageId); | ||
$this->pageFieldTypeGateway->removeZone((int) $attribute['zone_id']); | ||
} | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/lib/Persistence/Legacy/Content/Gateway/PageFieldTypeGatewayInterface.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MateuszBieniek\EzPlatformDatabaseHealthChecker\Persistence\Legacy\Content\Gateway; | ||
|
||
interface PageFieldTypeGatewayInterface | ||
{ | ||
public function countOrphanedPageRelations(): int; | ||
|
||
public function getOrphanedPageRelations(int $limit): array; | ||
} |