|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © OpenGento, All rights reserved. |
| 4 | + * See LICENSE bundled with this library for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Opengento\DocumentWidget\ViewModel; |
| 9 | + |
| 10 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\View\Element\Block\ArgumentInterface; |
| 13 | +use Opengento\Document\Api\Data\DocumentTypeInterface; |
| 14 | +use Opengento\Document\Api\DocumentTypeRepositoryInterface; |
| 15 | +use Psr\Log\LoggerInterface; |
| 16 | + |
| 17 | +final class DocumentType implements ArgumentInterface |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var DocumentTypeRepositoryInterface |
| 21 | + */ |
| 22 | + private $docTypeRepository; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var SearchCriteriaBuilder |
| 26 | + */ |
| 27 | + private $criteriaBuilder; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var LoggerInterface |
| 31 | + */ |
| 32 | + private $logger; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var DocumentTypeInterface[] |
| 36 | + */ |
| 37 | + private $documentTypes; |
| 38 | + |
| 39 | + /** |
| 40 | + * @var string[] |
| 41 | + */ |
| 42 | + private $visibilities; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + DocumentTypeRepositoryInterface $docTypeRepository, |
| 46 | + SearchCriteriaBuilder $criteriaBuilder, |
| 47 | + LoggerInterface $logger, |
| 48 | + array $visibilities |
| 49 | + ) { |
| 50 | + $this->docTypeRepository = $docTypeRepository; |
| 51 | + $this->criteriaBuilder = $criteriaBuilder; |
| 52 | + $this->logger = $logger; |
| 53 | + $this->visibilities = $visibilities; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return DocumentTypeInterface[] |
| 58 | + */ |
| 59 | + public function getList(): array |
| 60 | + { |
| 61 | + if (!$this->documentTypes) { |
| 62 | + $this->criteriaBuilder->addFilter('visibility', $this->visibilities, 'in'); |
| 63 | + try { |
| 64 | + $this->documentTypes = $this->docTypeRepository->getList($this->criteriaBuilder->create()); |
| 65 | + } catch (LocalizedException $e) { |
| 66 | + $this->logger->error($e->getLogMessage(), $e->getTrace()); |
| 67 | + |
| 68 | + $this->documentTypes = []; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $this->documentTypes->getItems(); |
| 73 | + } |
| 74 | +} |
0 commit comments