Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ConsoleCliStorage | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getHomePath | n/a |
0 / 0 |
n/a |
0 / 0 |
6 | |||||
| getRootPath | n/a |
0 / 0 |
n/a |
0 / 0 |
4 | |||||
| store | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| restore | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| remove | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\Console; |
| 4 | |
| 5 | use Apie\Core\Other\FileReaderInterface; |
| 6 | use Apie\Core\Other\FileWriterInterface; |
| 7 | |
| 8 | final class ConsoleCliStorage |
| 9 | { |
| 10 | private ?string $rootPath = null; |
| 11 | |
| 12 | private ?string $homePath = null; |
| 13 | |
| 14 | public function __construct( |
| 15 | private readonly FileWriterInterface&FileReaderInterface $fileWriter |
| 16 | ) { |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * @codeCoverageIgnore |
| 21 | */ |
| 22 | private function getHomePath(): string |
| 23 | { |
| 24 | if ($this->homePath) { |
| 25 | return $this->homePath; |
| 26 | } |
| 27 | $home = getenv('HOME'); |
| 28 | if (empty($home)) { |
| 29 | if (!empty($_SERVER['HOMEDRIVE']) && !empty($_SERVER['HOMEPATH'])) { |
| 30 | // home on windows |
| 31 | $home = $_SERVER['HOMEDRIVE'] . $_SERVER['HOMEPATH']; |
| 32 | } |
| 33 | } |
| 34 | return $this->homePath = empty($home) ? $this->getRootPath() : $home; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @codeCoverageIgnore |
| 39 | */ |
| 40 | private function getRootPath(): string |
| 41 | { |
| 42 | if ($this->rootPath) { |
| 43 | return $this->rootPath; |
| 44 | } |
| 45 | $bt = debug_backtrace(); |
| 46 | if (isset($bt[0]['file'])) { |
| 47 | return $this->rootPath = $bt[0]['file']; |
| 48 | } |
| 49 | // fallback |
| 50 | $files = get_included_files(); |
| 51 | return $this->rootPath = reset($files) ? : __FILE__; |
| 52 | } |
| 53 | public function store(string $key, string $value): void |
| 54 | { |
| 55 | $filePath = $this->getHomePath() . '/.apie-' . md5($this->getRootPath() . '...' . $key) . '-cli'; |
| 56 | $this->fileWriter->writeFile($filePath, $value); |
| 57 | } |
| 58 | |
| 59 | public function restore(string $key): ?string |
| 60 | { |
| 61 | $filePath = $this->getHomePath() . '/.apie-' . md5($this->getRootPath() . '...' . $key) . '-cli'; |
| 62 | return $this->fileWriter->fileExists($filePath) ? $this->fileWriter->readContents($filePath) : null; |
| 63 | } |
| 64 | |
| 65 | public function remove(string $key): void |
| 66 | { |
| 67 | $filePath = $this->getHomePath() . '/.apie-' . md5($this->getRootPath() . '...' . $key) . '-cli'; |
| 68 | $this->fileWriter->clearPath($filePath); |
| 69 | } |
| 70 | } |