Passed
Push — master ( 990f35...a2e3d3 )
by Marcel
03:12
created

VariableService::replaceTextVariables()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 23
rs 8.8333
1
<?php
2
/**
3
 * Analytics
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @copyright 2021 Marcel Scherello
10
 */
11
12
namespace OCA\Analytics\Service;
13
14
use OCA\Analytics\Db\DatasetMapper;
15
use Psr\Log\LoggerInterface;
16
17
class VariableService
18
{
19
    private $logger;
20
    private $DatasetMapper;
21
22
    public function __construct(
23
        LoggerInterface $logger,
24
        DatasetMapper $DatasetMapper
25
    )
26
    {
27
        $this->logger = $logger;
28
        $this->DatasetMapper = $DatasetMapper;
29
    }
30
31
    /**
32
     * replace %*% text variables in name and subheader
33
     *
34
     * @param array $datasetMetadata
35
     * @return array
36
     */
37
    public function replaceTextVariables($datasetMetadata)
38
    {
39
        $fields = ['name', 'subheader'];
40
        foreach ($fields as $field) {
41
            $name = $datasetMetadata[$field];
42
43
            preg_match_all("/%.*?%/", $name, $matches);
44
            if (count($matches[0]) > 0) {
45
                foreach ($matches[0] as $match) {
46
                    if ($match === '%currentDate%') {
47
                        $replace = date("m.d.y");
48
                    } elseif ($match === '%lastUpdate%') {
49
                        $timestamp = $this->DatasetMapper->getLastUpdate($datasetMetadata['id']);
50
                        $replace = date('d.m.Y', $timestamp);
51
                    } elseif ($match === '%owner%') {
52
                        $owner = $this->DatasetMapper->getOwner($datasetMetadata['id']);
53
                        $replace = $owner;
54
                    }
55
                    $datasetMetadata[$field] = preg_replace('/' . $match . '/', $replace, $datasetMetadata[$field]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $replace does not seem to be defined for all execution paths leading up to this point.
Loading history...
56
                }
57
            }
58
        }
59
        return $datasetMetadata;
60
    }
61
}