Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
DataStorageTrait
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
8 / 8
14
100.00% covered (success)
100.00%
1 / 1
 getStoredImageUploadIds
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getStoredFileUploadIds
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 replaceImagePaths
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 replaceFilePaths
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getFileHref
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 getImagesPathForStorage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFilesPathForStorage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getEntityPathForStorage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Olz\Entity\Common;
4
5use Olz\Utils\WithUtilsTrait;
6
7trait DataStorageTrait {
8    use WithUtilsTrait;
9
10    /** @return array<non-empty-string> */
11    public function getStoredImageUploadIds(): array {
12        $img_path = $this->getImagesPathForStorage();
13        return $this->uploadUtils()->getStoredUploadIds("{$img_path}img/");
14    }
15
16    /** @return array<non-empty-string> */
17    public function getStoredFileUploadIds(): array {
18        return $this->uploadUtils()->getStoredUploadIds($this->getFilesPathForStorage());
19    }
20
21    public function replaceImagePaths(string $html): string {
22        $entity_name = $this::getEntityNameForStorage();
23        $entity_id = $this->getEntityIdForStorage();
24        $upload_ids = $this->getStoredImageUploadIds();
25        foreach ($upload_ids as $upload_id) {
26            $search1 = "<img src=\"./{$upload_id}\" alt=\"\">";
27            $search2 = "<img src=\"./{$upload_id}\" alt=\"\" />";
28            $replace = $this->imageUtils()->olzImage($entity_name, $entity_id, $upload_id, 256, 'image');
29            $html = str_replace($search1, $replace, $html);
30            $html = str_replace($search2, $replace, $html);
31        }
32        return $html;
33    }
34
35    public function replaceFilePaths(string $html): string {
36        $data_path = $this->envUtils()->getDataPath();
37        $data_href = $this->envUtils()->getDataHref();
38        $entity_path = $this->getEntityPathForStorage();
39        $upload_ids = $this->getStoredFileUploadIds();
40        foreach ($upload_ids as $upload_id) {
41            $file_path = "{$data_path}files/{$entity_path}{$upload_id}";
42            $file_href = "{$data_href}files/{$entity_path}{$upload_id}";
43            $filemtime = is_file($file_path) ? filemtime($file_path) : false;
44            $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : '';
45            $search = "\"./{$upload_id}\"";
46            $replace = "\"{$file_href}?modified={$modified}\"";
47            $html = str_replace($search, $replace, $html);
48        }
49        return $html;
50    }
51
52    public function getFileHref(string $upload_id): string {
53        $data_path = $this->envUtils()->getDataPath();
54        $data_href = $this->envUtils()->getDataHref();
55        $entity_path = $this->getEntityPathForStorage();
56        $file_path = "{$data_path}files/{$entity_path}{$upload_id}";
57        $file_href = "{$data_href}files/{$entity_path}{$upload_id}";
58        $filemtime = is_file($file_path) ? filemtime($file_path) : false;
59        $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : '';
60        return "{$file_href}?modified={$modified}";
61    }
62
63    /** @return non-empty-string */
64    public function getImagesPathForStorage(): string {
65        $data_path = $this->envUtils()->getDataPath();
66        $entity_path = $this->getEntityPathForStorage();
67        return "{$data_path}img/{$entity_path}";
68    }
69
70    /** @return non-empty-string */
71    public function getFilesPathForStorage(): string {
72        $data_path = $this->envUtils()->getDataPath();
73        $entity_path = $this->getEntityPathForStorage();
74        return "{$data_path}files/{$entity_path}";
75    }
76
77    /** @return non-empty-string */
78    public function getEntityPathForStorage(): string {
79        $entity_name = $this::getEntityNameForStorage();
80        $entity_id = $this->getEntityIdForStorage();
81        return "{$entity_name}/{$entity_id}/";
82    }
83}