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

Publisher::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\Docweaver\Service\Documentation;
6
7
use Illuminate\Console\Command;
8
use ReliqArts\Docweaver\Contract\ConfigProvider;
9
use ReliqArts\Docweaver\Contract\Documentation\Publisher as PublisherContract;
10
use ReliqArts\Docweaver\Contract\Exception;
11
use ReliqArts\Docweaver\Contract\Filesystem;
12
use ReliqArts\Docweaver\Contract\Logger;
13
use ReliqArts\Docweaver\Contract\Product\Maker as ProductFactory;
14
use ReliqArts\Docweaver\Contract\Product\Publisher as ProductPublisher;
15
use ReliqArts\Docweaver\Exception\BadImplementation;
16
use ReliqArts\Docweaver\Result;
17
use ReliqArts\Docweaver\Service\Publisher as BasePublisher;
18
19
/**
20
 * Publishes and updates documentation.
21
 */
22
final class Publisher extends BasePublisher implements PublisherContract
23
{
24
    /**
25
     * Documentation resource directory.
26
     *
27
     * @var string
28
     */
29
    private string $documentationDirectory;
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...
30
31
    /**
32
     * @var ProductPublisher
33
     */
34
    private ProductPublisher $productPublisher;
35
36
    /**
37
     * @var ProductFactory
38
     */
39
    private ProductFactory $productFactory;
40
41
    /**
42
     * Working directory.
43
     *
44
     * @var string
45
     */
46
    private string $workingDirectory;
47
48
    /**
49
     * Create a new DocumentationPublisher.
50
     *
51
     * @throws BadImplementation
52
     */
53
    public function __construct(
54
        Filesystem $filesystem,
55
        Logger $logger,
56
        ConfigProvider $configProvider,
57
        ProductPublisher $productPublisher,
58
        ProductFactory $productFactory
59
    ) {
60
        parent::__construct($filesystem, $logger);
61
62
        $this->productPublisher = $productPublisher;
63
        $this->productFactory = $productFactory;
64
        $this->documentationDirectory = $configProvider->getDocumentationDirectory();
65
        $this->workingDirectory = base_path($this->documentationDirectory);
66
67
        if (!$this->readyResourceDirectory($this->workingDirectory)) {
68
            throw new BadImplementation(sprintf('Could not ready document resource directory `%s`. Please ensure file system is writable.', $this->documentationDirectory));
69
        }
70
    }
71
72
    /**
73
     * @throws Exception
74
     */
75
    public function publish(string $productName, string $source = '', Command &$callingCommand = null): Result
76
    {
77
        $result = new Result();
78
        $commonName = strtolower($productName);
79
        $productDirectory = sprintf('%s/%s', $this->workingDirectory, $commonName);
80
81
        $this->setExecutionStartTime();
82
        if (!$this->readyResourceDirectory($productDirectory)) {
83
            return $result->setError(sprintf('Product directory %s is not writable.', $productDirectory))
84
                ->setExtra((object)['executionTime' => $this->getExecutionTime()]);
85
        }
86
87
        $product = $this->productFactory->create($productDirectory);
88
        $result = $this->productPublisher->publish($product, $source);
89
90
        foreach ($result->getMessages() as $message) {
91
            $this->tell($message);
92
        }
93
94
        $data = $result->getExtra() ?? (object)[];
95
        $data->executionTime = $this->getExecutionTime();
96
97
        return $result->setExtra($data);
98
    }
99
100
    /**
101
     * @throws Exception
102
     */
103
    public function update(string $productName, Command &$callingCommand = null): Result
104
    {
105
        $this->callingCommand = $callingCommand;
106
        $result = new Result();
107
        $commonName = strtolower($productName);
108
        $productDirectory = sprintf('%s/%s', $this->workingDirectory, $commonName);
109
110
        $this->setExecutionStartTime();
111
        if (!$this->readyResourceDirectory($productDirectory)) {
112
            return $result->setError(sprintf('Product directory %s is not writable.', $productDirectory))
113
                ->setExtra((object)['executionTime' => $this->getExecutionTime()]);
114
        }
115
116
        $product = $this->productFactory->create($productDirectory);
117
        $result = $this->productPublisher->update($product);
118
119
        foreach ($result->getMessages() as $message) {
120
            $this->tell($message);
121
        }
122
123
        $data = $result->getExtra() ?? (object)[];
124
        $data->executionTime = $this->getExecutionTime();
125
126
        return $result->setExtra($data);
127
    }
128
129
    /**
130
     * @throws Exception
131
     */
132
    public function updateAll(Command &$callingCommand = null): Result
133
    {
134
        $this->callingCommand = $callingCommand;
135
        $result = new Result();
136
        $productDirectories = $this->filesystem->directories($this->workingDirectory);
137
        $products = [];
138
        $productsUpdated = [];
139
140
        $this->setExecutionStartTime();
141
142
        foreach ($productDirectories as $productDirectory) {
143
            $productName = basename($productDirectory);
144
145
            $this->tell(sprintf('Updating %s...', $productName), self::TELL_DIRECTION_FLAT);
146
147
            $productResult = $this->update($productName, $callingCommand);
148
            $products[] = $productName;
149
150
            if ($productResult->isSuccess()) {
151
                $productsUpdated[] = $productName;
152
            }
153
        }
154
155
        return $result->setExtra((object)[
156
            'products' => $products,
157
            'productsUpdated' => $productsUpdated,
158
            'executionTime' => $this->getExecutionTime(),
159
        ]);
160
    }
161
}
162