![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-media-gallery-ui/Model/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ declare(strict_types=1); namespace Magento\MediaGalleryUi\Model; use Magento\Backend\Model\UrlInterface; use Magento\Framework\Exception\IntegrationException; use Magento\MediaContentApi\Api\GetContentByAssetIdsInterface; /** * Provide information on which content asset is used in */ class GetAssetUsageDetails { /** * @var GetContentByAssetIdsInterface */ private $getContent; /** * @var UrlInterface */ private $url; /** * @var array */ private $contentTypes; /** * @param GetContentByAssetIdsInterface $getContent * @param UrlInterface $url * @param array $contentTypes */ public function __construct( GetContentByAssetIdsInterface $getContent, UrlInterface $url, array $contentTypes = [] ) { $this->getContent = $getContent; $this->url = $url; $this->contentTypes = $contentTypes; } /** * Provide information on which content asset is used in * * @param int $id * @return array * @throws IntegrationException */ public function execute(int $id): array { $details = []; foreach ($this->getUsageByEntities($id) as $type => $entities) { $details[] = [ 'name' => $this->getName($type), 'number' => count($entities), 'link' => $this->getLinkUrl($type) ]; } return $details; } /** * Retrieve the type name from content types configuration * * @param string $type * @return string */ private function getName(string $type): string { if (isset($this->contentTypes[$type]) && !empty($this->contentTypes[$type]['name'])) { return $this->contentTypes[$type]['name']; } return $type; } /** * Retrieve the type link from content types configuration * * @param string $type * @return string|null */ private function getLinkUrl(string $type): ?string { if (isset($this->contentTypes[$type]) && !empty($this->contentTypes[$type]['link'])) { return $this->url->getUrl($this->contentTypes[$type]['link']); } return null; } /** * Get used in counts per type * * @param int $assetId * @return int[] * @throws IntegrationException */ private function getUsageByEntities(int $assetId): array { $usage = []; foreach ($this->getContent->execute([$assetId]) as $contentIdentity) { $id = $contentIdentity->getEntityId(); $type = $contentIdentity->getEntityType(); $usage[$type][$id] = isset($usage[$type][$id]) ? $usage[$type][$id]++ : 0; } return $usage; } }