Transaction::toRequest()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.9666
cc 3
nc 2
nop 2
crap 3
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2020 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
declare(strict_types=1);
11
12
namespace ArangoDb\Type;
13
14
use ArangoDb\Guard\Guard;
15
use ArangoDb\Http\Url;
16
use ArangoDb\Util\Json;
17
use Fig\Http\Message\RequestMethodInterface;
18
use Psr\Http\Message\RequestFactoryInterface;
19
use Psr\Http\Message\RequestInterface;
20
use Psr\Http\Message\StreamFactoryInterface;
21
22
final class Transaction implements TransactionType
23
{
24
    /**
25
     * @var bool
26
     */
27
    private $waitForSync;
28
29
    /**
30
     * @var array
31
     */
32
    private $collections;
33
34
    /**
35
     * @var array
36
     */
37
    private $params;
38
39
    /**
40
     * @var string
41
     */
42
    private $action;
43
44
    /**
45
     * Guard
46
     *
47
     * @var Guard
48
     */
49
    private $guard;
50
51 7
    private function __construct(
52
        string $action,
53
        array $collections,
54
        array $params,
55
        bool $waitForSync = false
56
    ) {
57 7
        $this->action = $action;
58 7
        $this->collections = $collections;
59 7
        $this->params = $params;
60 7
        $this->waitForSync = $waitForSync;
61 7
    }
62
63 7
    public static function with(
64
        string $action,
65
        array $write,
66
        array $params = [],
67
        array $read = [],
68
        bool $waitForSync = false
69
    ): TransactionType {
70 7
        return new self(
71 7
            $action,
72
            [
73 7
                'write' => $write,
74 7
                'read' => $read,
75
            ],
76
            $params,
77
            $waitForSync
78
        );
79
    }
80
81 7
    public function toRequest(
82
        RequestFactoryInterface $requestFactory,
83
        StreamFactoryInterface $streamFactory
84
    ): RequestInterface {
85 7
        $collections = $this->collections;
86
87 7
        if (isset($collections['read']) && 0 === count($collections['read'])) {
88 7
            unset($collections['read']);
89
        }
90 7
        $request = $requestFactory->createRequest(RequestMethodInterface::METHOD_POST, Url::TRANSACTION);
91
92
93 7
        return $request->withBody($streamFactory->createStream(Json::encode(
94
            [
95 7
                'action' => $this->action,
96 7
                'collections' => $collections,
97 7
                'params' => $this->params,
98 7
                'waitForSync' => $this->waitForSync,
99
            ]
100
        )));
101
    }
102
103
    public function useGuard(Guard $guard): Type
104
    {
105
        $this->guard = $guard;
106
        return $this;
107
    }
108
109 7
    public function guard(): ?Guard
110
    {
111 7
        return $this->guard;
112
    }
113
}
114