OpenFoodFactsApiWrapper::setupApi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
1
<?php
2
3
namespace OpenFoodFacts\Laravel;
4
5
use GuzzleHttp\Client;
6
use OpenFoodFacts\Api;
7
use Psr\SimpleCache\CacheInterface;
8
9
class OpenFoodFactsApiWrapper
10
{
11
    public readonly Api $api;
12
13 30
    public function __construct(
14
        public readonly array $parameters,
15
        protected readonly ?CacheInterface $cache = null,
16
        string $environment = 'food'
17
    ) {
18 30
        $this->api = $this->setupApi($environment);
0 ignored issues
show
Bug introduced by
The property api is declared read-only in OpenFoodFacts\Laravel\OpenFoodFactsApiWrapper.
Loading history...
19
    }
20
21 30
    protected function setupApi(string $environment): Api
22
    {
23 30
        return new Api(
24 30
            $environment,
25 30
            $this->parameters['geography'] ?? 'world',
26 30
            null,
27 30
            $this->httpClient(),
28 30
            $this->cache
29 30
        );
30
    }
31
32 30
    protected function httpClient(): Client
33
    {
34 30
        return new Client([
35 30
            'headers' => ['User-Agent' => $this->parameters['app'] ?? 'Laravel Open Food Facts - https://github.com/openfoodfacts/openfoodfacts-laravel'],
36 30
        ]);
37
    }
38
}
39