• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / ares / 8758387852

05 Apr 2024 07:38AM UTC coverage: 89.112% (-0.2%) from 89.275%
8758387852

push

github

h4kuna
feat(AresFactory): improve

13 of 14 new or added lines in 1 file covered. (92.86%)

311 of 349 relevant lines covered (89.11%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.31
/src/AresFactory.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Ares;
4

5
use GuzzleHttp;
6
use h4kuna\Ares\Adis\StatusBusinessSubjects\StatusBusinessSubjectsTransformer;
7
use h4kuna\Ares\Exceptions\InvalidStateException;
8
use h4kuna\Ares\Http\HttpFactory;
9
use h4kuna\Ares\Http\TransportProvider;
10
use Psr\Http\Client\ClientInterface;
11
use Psr\Http\Message\RequestFactoryInterface;
12
use Psr\Http\Message\StreamFactoryInterface;
13

14
/**
15
 * @phpstan-type multiFactory RequestFactoryInterface&StreamFactoryInterface
16
 */
17
class AresFactory
18
{
19
        /**
20
         * @var multiFactory|null
21
         */
22
        private null|RequestFactoryInterface|StreamFactoryInterface $multiFactory = null;
23

24

25
        public function __construct(
1✔
26
                private ?ClientInterface $client = null,
27
                private ?StreamFactoryInterface $streamFactory = null,
28
                private ?RequestFactoryInterface $requestFactory = null,
29
        )
30
        {
31
        }
1✔
32

33

34
        public function create(): Ares
35
        {
36
                $streamFactory = $this->getStreamFactory();
1✔
37
                $transportProvider = $this->createTransportProvider($streamFactory);
1✔
38
                $adisContentProvider = $this->createAdisContentProvider($transportProvider);
1✔
39
                $aresClient = new Ares\Client($transportProvider);
1✔
40

41
                $dataBoxClient = new DataBox\Client($transportProvider);
1✔
42
                $dataBoxContentProvider = new DataBox\ContentProvider($dataBoxClient, $streamFactory);
1✔
43

44
                return new Ares($aresClient, $dataBoxContentProvider, $adisContentProvider);
1✔
45
        }
46

47

48
        public function getRequestFactory(): RequestFactoryInterface
49
        {
50
                return $this->requestFactory ??= $this->getMultiFactory();
1✔
51
        }
52

53

54
        public function getClient(): ClientInterface
55
        {
56
                if ($this->client !== null) {
1✔
NEW
57
                        return $this->client;
×
58
                }
59
                self::checkGuzzle();
1✔
60

61
                return $this->client = new GuzzleHttp\Client();
1✔
62
        }
63

64

65
        public function getStreamFactory(): StreamFactoryInterface
66
        {
67
                return $this->streamFactory ??= $this->getMultiFactory();
1✔
68
        }
69

70

71
        protected function createAdisContentProvider(TransportProvider $transportProvider): Adis\ContentProvider
1✔
72
        {
73
                return new Adis\ContentProvider(new Adis\Client($transportProvider), new StatusBusinessSubjectsTransformer());
1✔
74
        }
75

76

77
        public function createTransportProvider(StreamFactoryInterface $streamFactory): TransportProvider
1✔
78
        {
79
                $client = $this->getClient();
1✔
80
                $requestFactory = $this->getRequestFactory();
1✔
81
                return new TransportProvider($requestFactory, $client, $streamFactory);
1✔
82
        }
83

84

85
        /**
86
         * @return multiFactory
87
         */
88
        protected function getMultiFactory(): RequestFactoryInterface|StreamFactoryInterface
89
        {
90
                self::checkGuzzle();
1✔
91

92
                return $this->multiFactory ??= class_exists(GuzzleHttp\Psr7\HttpFactory::class) ? new GuzzleHttp\Psr7\HttpFactory() : new HttpFactory();
1✔
93
        }
94

95

96
        private static function checkGuzzle(): void
97
        {
98
                if (!class_exists(GuzzleHttp\Client::class)) {
1✔
99
                        throw new InvalidStateException('Guzzle not found, let implement own solution or install guzzle by: composer require guzzlehttp/guzzle');
×
100
                }
101
        }
1✔
102

103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc