Passed
Push — master ( 653498...1099cb )
by Patrick
03:08
created

ForecastApi::warmTaskCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
namespace ForecastAutomation\ForecastClient\Business;
4
5
use ForecastAutomation\Activity\Shared\Dto\ActivityDtoCollection;
6
use ForecastAutomation\ForecastClient\Shared\Dto\ForecastConfigDto;
7
use GuzzleHttp\Client;
8
9
class ForecastApi
10
{
11
    private const TASK_LIST_ENDPOINT = '/api/v2/projects/{{PROJECT_ID}}/tasks', TIME_REGISTRATIONS_ENDPOINT = '/api/v1/time_registrations';
12
13
    public static array $TASK_CACHE = [];
14
15 1
    public function __construct(private Client $guzzleClient, private ForecastConfigDto $forecastConfigDto)
16
    {
17
        //ToDo: Use \Psr\SimpleCache\CacheInterface $this->cache->set($cacheKey, $result, 6000)
18 1
        $this->warmTaskCache();
19 1
    }
20
21 1
    private function warmTaskCache(): array
22
    {
23 1
        if (count(static::$TASK_CACHE) === 0) {
24 1
            static::$TASK_CACHE = $this->callGetApi(
25 1
                str_replace('{{PROJECT_ID}}', $this->forecastConfigDto->forecastProjectId, self::TASK_LIST_ENDPOINT)
26
            );
27
        }
28
29 1
        return static::$TASK_CACHE;
30
    }
31
32 1
    private function callGetApi(string $path)
33
    {
34 1
        $res = $this->guzzleClient->request(
35 1
            'GET',
36
            $path,
37
            [
38 1
                'headers' => ['X-FORECAST-API-KEY' => $this->forecastConfigDto->forecastApiKey],
39
            ]
40
        );
41
42 1
        return \json_decode($res->getBody(), null, 512, JSON_THROW_ON_ERROR);
43
    }
44
45 1
    private function callPostApi(string $path, array $postData)
46
    {
47 1
        $res = $this->guzzleClient->request(
48 1
            'POST',
49
            $path,
50
            [
51
                'headers' => [
52 1
                    'X-FORECAST-API-KEY' => $this->forecastConfigDto->forecastApiKey,
53 1
                    'Content-Type' => 'application/json',
54 1
                    'Accept' => 'application/json',
55
                ],
56 1
                'body' => \json_encode($postData, JSON_THROW_ON_ERROR),
57
            ]
58
        );
59
60 1
        return \json_decode($res->getBody(), null, 512, JSON_THROW_ON_ERROR);
61
    }
62
63 1
    public function writeActivities(ActivityDtoCollection $activityDtoCollection): int
64
    {
65 1
        $savedActivities = 0;
66 1
        foreach ($activityDtoCollection as $activityDto) {
67 1
            $writeTimeRegistration = [
68 1
                'person' => (int)$this->forecastConfigDto->forecastPersonId,
69 1
                'task' => $this->findTaskIdToNeedle($activityDto->needle),
70 1
                'time_registered' => $activityDto->duration,
71 1
                'date' => $activityDto->created->format('Y-m-d'),
72 1
                'notes' => $activityDto->description,
73
            ];
74 1
            $writeResponse = $this->callPostApi(self::TIME_REGISTRATIONS_ENDPOINT, $writeTimeRegistration);
0 ignored issues
show
Unused Code introduced by
The assignment to $writeResponse is dead and can be removed.
Loading history...
75
            //ToDo: return output or use loggertrait, otherwise not testable
76
//            echo "New Time Entry (date: $writeResponse->date, person: $writeResponse->person, notes: $writeResponse->notes \n";
77 1
            $savedActivities++;
78
        }
79
80 1
        return $savedActivities;
81
    }
82
83 1
    private function findTaskIdToNeedle(string $taskNeedle): int
84
    {
85 1
        foreach (static::$TASK_CACHE as $task) {
86 1
            if (\str_contains($task->title, $taskNeedle)) {
87 1
                return $task->id;
88
            }
89
        }
90
91
        return (int)$this->forecastConfigDto->forecastFallbackTaskId;
92
    }
93
}
94