Lines 77.27% 17 / 22
Methods 87.50% 7 / 8
Classes 0.00% 0 / 1
Covered by tests of size
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 getType 100.00% 1 / 1 100.00% 1 / 1 1
 getDescription 100.00% 3 / 3 100.00% 1 / 1 1
 getSource 100.00% 1 / 1 100.00% 1 / 1 1
 getDestination 100.00% 1 / 1 100.00% 1 / 1 1
 execute 0.00% 0 / 5 0.00% 0 / 1 20
 toArray 100.00% 7 / 7 100.00% 1 / 1 1
 shortenPath 100.00% 3 / 3 100.00% 1 / 1 2
24readonly class DirectoryCopyOperation implements OperationInterface
25{
26    public function __construct(
27        private Filesystem $filesystem,
28        private string $source,
29        private string $destination,
30        private bool $onlyIfExists = true,
31    ) {
32    }
33
34    public function getType(): string
35    {
36        return 'directory_copy';
37    }
38
39    public function getDescription(): string
40    {
41        $sourceShort = $this->shortenPath($this->source);
42        $destShort = $this->shortenPath($this->destination);
43        return sprintf('Copy directory: %s -> %s', $sourceShort, $destShort);
44    }
45
46    public function getSource(): string
47    {
48        return $this->source;
49    }
50
51    public function getDestination(): string
52    {
53        return $this->destination;
54    }
55
56    public function execute(): bool
57    {
58        if ($this->onlyIfExists && !is_dir($this->source)) {
59            return true; // Skip if a source doesn't exist and onlyIfExists is true
60        }
61
62        try {
63            return $this->filesystem->recursiveCopy($this->source, $this->destination);
64        } catch (\Exception) {
65            return false;
66        }
67    }
68
69    public function toArray(): array
70    {
71        return [
72            'type' => $this->getType(),
73            'description' => $this->getDescription(),
74            'source' => $this->source,
75            'destination' => $this->destination,
76            'onlyIfExists' => $this->onlyIfExists,
77        ];
78    }
79
80    private function shortenPath(string $path): string
81    {
82        // Remove common prefixes to shorten the path for display
83        if (defined('PMF_ROOT_DIR')) {
84            $path = str_replace((string) PMF_ROOT_DIR, replace: '', subject: $path);
85        }
86        return $path;
87    }
88}