Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
DataStorageTrait
100.00% covered (success)
100.00%
41 / 41
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%
8 / 8
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            $search = "<img src=\"./{$upload_id}\" alt=\"\" />";
27            $replace = $this->imageUtils()->olzImage($entity_name, $entity_id, $upload_id, 110, 'image');
28            $html = str_replace($search, $replace, $html);
29        }
30        return $html;
31    }
32
33    public function replaceFilePaths(string $html): string {
34        $data_path = $this->envUtils()->getDataPath();
35        $data_href = $this->envUtils()->getDataHref();
36        $entity_path = $this->getEntityPathForStorage();
37        $upload_ids = $this->getStoredFileUploadIds();
38        foreach ($upload_ids as $upload_id) {
39            $file_path = "{$data_path}files/{$entity_path}{$upload_id}";
40            $file_href = "{$data_href}files/{$entity_path}{$upload_id}";
41            $filemtime = is_file($file_path) ? filemtime($file_path) : false;
42            $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : '';
43            $search = "\"./{$upload_id}\"";
44            $replace = "\"{$file_href}?modified={$modified}\"";
45            $html = str_replace($search, $replace, $html);
46        }
47        return $html;
48    }
49
50    public function getFileHref(string $upload_id): string {
51        $data_path = $this->envUtils()->getDataPath();
52        $data_href = $this->envUtils()->getDataHref();
53        $entity_path = $this->getEntityPathForStorage();
54        $file_path = "{$data_path}files/{$entity_path}{$upload_id}";
55        $file_href = "{$data_href}files/{$entity_path}{$upload_id}";
56        $filemtime = is_file($file_path) ? filemtime($file_path) : false;
57        $modified = is_int($filemtime) ? date('Y-m-d_H-i-s', $filemtime) : '';
58        return "{$file_href}?modified={$modified}";
59    }
60
61    /** @return non-empty-string */
62    public function getImagesPathForStorage(): string {
63        $data_path = $this->envUtils()->getDataPath();
64        $entity_path = $this->getEntityPathForStorage();
65        return "{$data_path}img/{$entity_path}";
66    }
67
68    /** @return non-empty-string */
69    public function getFilesPathForStorage(): string {
70        $data_path = $this->envUtils()->getDataPath();
71        $entity_path = $this->getEntityPathForStorage();
72        return "{$data_path}files/{$entity_path}";
73    }
74
75    /** @return non-empty-string */
76    public function getEntityPathForStorage(): string {
77        $entity_name = $this::getEntityNameForStorage();
78        $entity_id = $this->getEntityIdForStorage();
79        return "{$entity_name}/{$entity_id}/";
80    }
81}