Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 22 | final class Publisher extends BasePublisher implements PublisherContract |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Documentation resource directory. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private string $documentationDirectory; |
||
|
|
|||
| 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 |