Completed
Push — master ( 59ebde...3174b5 )
by ReliQ
01:08
created

Finder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 72
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Service\Product;
6
7
use ReliqArts\Docweaver\Contract\ConfigProvider;
8
use ReliqArts\Docweaver\Contract\Exception;
9
use ReliqArts\Docweaver\Contract\Filesystem;
10
use ReliqArts\Docweaver\Contract\Logger;
11
use ReliqArts\Docweaver\Contract\Product\Finder as FinderContract;
12
use ReliqArts\Docweaver\Contract\Product\Maker as ProductFactory;
13
use ReliqArts\Docweaver\Model\Product;
14
15
final class Finder implements FinderContract
16
{
17
    /**
18
     * @var Filesystem
19
     */
20
    private Filesystem $filesystem;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
21
22
    /**
23
     * @var Logger
24
     */
25
    private Logger $logger;
26
27
    /**
28
     * @var ConfigProvider
29
     */
30
    private ConfigProvider $configProvider;
31
32
    /**
33
     * @var ProductFactory
34
     */
35
    private ProductFactory $productFactory;
36
37
    /**
38
     * ProductFinder constructor.
39
     */
40
    public function __construct(
41
        Filesystem $filesystem,
42
        Logger $logger,
43
        ConfigProvider $configProvider,
44
        ProductFactory $productFactory
45
    ) {
46
        $this->filesystem = $filesystem;
47
        $this->logger = $logger;
48
        $this->configProvider = $configProvider;
49
        $this->productFactory = $productFactory;
50
    }
51
52
    /**
53
     * @return Product[]
54
     */
55
    public function listProducts(bool $includeUnknowns = false): array
56
    {
57
        $products = [];
58
        $documentationDirectory = $this->configProvider->getDocumentationDirectory();
59
        $productDirectories = $this->filesystem->directories(base_path($documentationDirectory));
60
61
        foreach ($productDirectories as $productDirectory) {
62
            try {
63
                $product = $this->productFactory->create($productDirectory);
64
                if ($includeUnknowns || $product->getDefaultVersion() !== Product::VERSION_UNKNOWN) {
65
                    $products[$product->getKey()] = $product;
66
                }
67
            } catch (Exception $e) {
68
                $this->logger->error($e->getMessage(), []);
69
            }
70
        }
71
72
        return $products;
73
    }
74
75
    public function findProduct(string $productName): ?Product
76
    {
77
        $productKey = strtolower($productName);
78
        $products = $this->listProducts();
79
80
        if (!array_key_exists($productKey, $products)) {
81
            return null;
82
        }
83
84
        return $products[$productKey];
85
    }
86
}
87