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 |
||
| 17 | abstract class ActionPostAbstract extends MiddlewareAbstract |
||
| 18 | { |
||
| 19 | const RECREATE_COMMAND = 'recreate'; |
||
| 20 | const URI_COMMAND = 'uri'; |
||
| 21 | const CURL_TIMEOUT = 15; |
||
| 22 | /** |
||
| 23 | * Generator provider |
||
| 24 | * @var mixed |
||
| 25 | */ |
||
| 26 | protected $generator; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var ResourceDOInterface|ResourceDO |
||
| 30 | */ |
||
| 31 | protected $resourceDO; |
||
| 32 | /** |
||
| 33 | * @var FilesystemInterface |
||
| 34 | */ |
||
| 35 | protected $filesystem; |
||
| 36 | |||
| 37 | public function __construct( |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param ServerRequestInterface $request |
||
| 47 | * @param ResponseInterface $response |
||
| 48 | * @param callable|null $next |
||
| 49 | * @return EmptyResponse |
||
| 50 | * @throws \Exception |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function __invoke( |
|
| 53 | ServerRequestInterface $request, |
||
| 54 | ResponseInterface $response, |
||
| 55 | callable $next = null |
||
| 56 | ) |
||
| 57 | { |
||
| 58 | parent::__invoke($request, $response, $next); |
||
| 59 | $this->response = $this->action(); |
||
| 60 | |||
| 61 | return $this->next(); |
||
| 62 | } |
||
| 63 | |||
| 64 | abstract protected function generate(ResourceDOInterface $resourceDO); |
||
| 65 | |||
| 66 | protected function action() |
||
| 67 | { |
||
| 68 | $headers = [ |
||
| 69 | 'Content-Type' => $this->resourceDO->getMimeType(), |
||
| 70 | ]; |
||
| 71 | $filePath = $this->resourceDO->getFilePath(); |
||
| 72 | $fileExists = is_file($filePath); |
||
| 73 | $recreate = PrepareResourceMiddlewareAbstract::getParamFromRequest(static::RECREATE_COMMAND, $this->request); |
||
| 74 | $uri = PrepareResourceMiddlewareAbstract::getParamFromRequest(static::URI_COMMAND, $this->request); |
||
| 75 | $recreate = $fileExists && $recreate; |
||
| 76 | $this->resourceDO->setNew(!$fileExists); |
||
| 77 | if (!$fileExists || $recreate) { |
||
| 78 | $this->resourceDO->setRecreate($recreate); |
||
| 79 | $upload = $this->upload(); |
||
| 80 | |||
| 81 | // Upload must be with high priority |
||
| 82 | if ($upload) { |
||
| 83 | |||
| 84 | /** @see \Zend\Diactoros\Response::$phrases */ |
||
| 85 | return new FileUploadedResponse($upload, 201, $headers); |
||
| 86 | } elseif ($uri) { |
||
| 87 | $upload = $this->download($this->resourceDO, $uri); |
||
| 88 | |||
| 89 | /** @see \Zend\Diactoros\Response::$phrases */ |
||
| 90 | return new FileUploadedResponse($upload, 201, $headers); |
||
| 91 | } else { |
||
| 92 | $body = $this->generate($this->resourceDO); |
||
| 93 | |||
| 94 | /** @see \Zend\Diactoros\Response::$phrases */ |
||
| 95 | return new FileContentResponse($body, 201, $headers); |
||
| 96 | } |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | /** @see \Zend\Diactoros\Response::$phrases */ |
||
| 101 | return new EmptyResponse(304, $headers); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @return UploadedFile|null |
||
| 106 | */ |
||
| 107 | protected function upload() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param ResourceDOInterface $resourceDO |
||
| 121 | * @param string $uri |
||
| 122 | * @return DownloadedFile |
||
| 123 | * @throws ErrorException |
||
| 124 | * @throws \Exception |
||
| 125 | */ |
||
| 126 | protected function download(ResourceDOInterface $resourceDO, $uri) |
||
| 181 | } |