Skip to content

Commit

Permalink
add documentype generic viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Dec 16, 2020
1 parent 5007c30 commit 3707464
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
74 changes: 74 additions & 0 deletions ViewModel/DocumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © OpenGento, All rights reserved.
* See LICENSE bundled with this library for license details.
*/
declare(strict_types=1);

namespace Opengento\DocumentWidget\ViewModel;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Opengento\Document\Api\Data\DocumentTypeInterface;
use Opengento\Document\Api\DocumentTypeRepositoryInterface;
use Psr\Log\LoggerInterface;

final class DocumentType implements ArgumentInterface
{
/**
* @var DocumentTypeRepositoryInterface
*/
private $docTypeRepository;

/**
* @var SearchCriteriaBuilder
*/
private $criteriaBuilder;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var DocumentTypeInterface[]
*/
private $documentTypes;

/**
* @var string[]
*/
private $visibilities;

public function __construct(
DocumentTypeRepositoryInterface $docTypeRepository,
SearchCriteriaBuilder $criteriaBuilder,
LoggerInterface $logger,
array $visibilities
) {
$this->docTypeRepository = $docTypeRepository;
$this->criteriaBuilder = $criteriaBuilder;
$this->logger = $logger;
$this->visibilities = $visibilities;
}

/**
* @return DocumentTypeInterface[]
*/
public function getList(): array
{
if (!$this->documentTypes) {
$this->criteriaBuilder->addFilter('visibility', $this->visibilities, 'in');
try {
$this->documentTypes = $this->docTypeRepository->getList($this->criteriaBuilder->create());
} catch (LocalizedException $e) {
$this->logger->error($e->getLogMessage(), $e->getTrace());

$this->documentTypes = [];
}
}

return $this->documentTypes->getItems();
}
}
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"roave/security-advisories": "dev-master"
},
"suggest": {
"opengento/module-document-product-link": "",
"opengento/module-document-product-search": "",
"opengento/module-document-search": ""
"opengento/module-document-product-link": "This module aims to help merchants to link their documents to products in Magento 2.",
"opengento/module-document-product-search": "This module aims to make documents searchable with product keywords in Magento 2.",
"opengento/module-document-search": "This module aims to make documents searchable for customers in Magento 2."
},
"license": [
"MIT"
Expand Down
7 changes: 7 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@
</argument>
</arguments>
</type>
<virtualType name="Opengento\DocumentWidget\ViewModel\VisibleDocumentType" type="Opengento\DocumentWidget\ViewModel\DocumentType">
<arguments>
<argument name="visibilities" xsi:type="array">
<item name="public" xsi:type="const">Opengento\Document\Model\DocumentType\Visibility::VISIBILITY_PUBLIC</item>
</argument>
</arguments>
</virtualType>
</config>

0 comments on commit 3707464

Please sign in to comment.