Completed
Push — master ( cd3029...c1bcb1 )
by Nate
05:05
created

AbstractProxy::createArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Proxy;
10
11
use Tebru\Retrofit\HttpClient;
12
use Tebru\Retrofit\Internal\HttpClientCall;
13
use Tebru\Retrofit\Internal\ServiceMethod\ServiceMethodFactory;
14
use Tebru\Retrofit\Proxy;
15
16
/**
17
 * Class Proxy
18
 *
19
 * @author Nate Brunette <[email protected]>
20
 */
21
abstract class AbstractProxy implements Proxy
22
{
23
    public const RETROFIT_NO_DEFAULT_VALUE = '__retrofit_no_default_value__';
24
25
    /**
26
     * Creates a [@see DefaultServiceMethod]
27
     *
28
     * @var ServiceMethodFactory
29
     */
30
    private $serviceMethodFactory;
31
32
    /**
33
     * A retrofit http client
34
     *
35
     * @var HttpClient
36
     */
37
    private $client;
38
39
    /**
40
     * Constructor
41
     *
42
     * @param ServiceMethodFactory $serviceMethodFactory
43
     * @param HttpClient $client
44
     */
45 20
    public function __construct(ServiceMethodFactory $serviceMethodFactory, HttpClient $client)
46
    {
47 20
        $this->serviceMethodFactory = $serviceMethodFactory;
48 20
        $this->client = $client;
49 20
    }
50
51
    /** @noinspection MagicMethodsValidityInspection */
52
    /**
53
     * Constructs a [@see Call] object based on an interface method and arguments, then passes it through a
54
     * [@see CallAdapter] before returning.
55
     *
56
     * @param string $interfaceName
57
     * @param string $methodName
58
     * @param array $args
59
     * @param array $defaultArgs
60
     * @return mixed
61
     */
62 15
    public function __handleRetrofitRequest(string $interfaceName, string $methodName, array $args, array $defaultArgs)
63
    {
64 15
        $args = $this->createArgs($args, $defaultArgs);
65 15
        $serviceMethod = $this->serviceMethodFactory->create($interfaceName, $methodName);
66
67 14
        return $serviceMethod->adapt(new HttpClientCall($this->client, $serviceMethod, $args));
68
    }
69
70
    /**
71
     * Append any default args to argument array
72
     *
73
     * @param array $args
74
     * @param array $defaultArgs
75
     * @return array
76
     */
77 15
    private function createArgs(array $args, array $defaultArgs): array
78
    {
79 15
        $numProvidedArgs = \count($args);
80 15
        $numArgs = \count($defaultArgs);
81
82 15
        if ($numArgs === $numProvidedArgs) {
83 13
            return $args;
84
        }
85
86
        // get arguments from end that were not provided
87 2
        $appendedArgs = \array_slice($defaultArgs, $numProvidedArgs);
88
89 2
        return \array_merge($args, $appendedArgs);
90
    }
91
}
92