Skip to content

Commit 3707464

Browse files
committed
add documentype generic viewmodel
1 parent 5007c30 commit 3707464

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

ViewModel/DocumentType.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"roave/security-advisories": "dev-master"
2424
},
2525
"suggest": {
26-
"opengento/module-document-product-link": "",
27-
"opengento/module-document-product-search": "",
28-
"opengento/module-document-search": ""
26+
"opengento/module-document-product-link": "This module aims to help merchants to link their documents to products in Magento 2.",
27+
"opengento/module-document-product-search": "This module aims to make documents searchable with product keywords in Magento 2.",
28+
"opengento/module-document-search": "This module aims to make documents searchable for customers in Magento 2."
2929
},
3030
"license": [
3131
"MIT"

etc/frontend/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,11 @@
4141
</argument>
4242
</arguments>
4343
</type>
44+
<virtualType name="Opengento\DocumentWidget\ViewModel\VisibleDocumentType" type="Opengento\DocumentWidget\ViewModel\DocumentType">
45+
<arguments>
46+
<argument name="visibilities" xsi:type="array">
47+
<item name="public" xsi:type="const">Opengento\Document\Model\DocumentType\Visibility::VISIBILITY_PUBLIC</item>
48+
</argument>
49+
</arguments>
50+
</virtualType>
4451
</config>

0 commit comments

Comments
 (0)