Passed
Push — master ( 898ee1...a00989 )
by Gregorio
02:43
created

Rosetta::item()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
namespace Ghc\Rosetta;
4
5
use Ghc\Rosetta\Connectors\Connector;
6
use Ghc\Rosetta\Connectors\Request;
7
use Ghc\Rosetta\Exceptions\ManagerException;
8
use Ghc\Rosetta\Messages\Message;
9
use Ghc\Rosetta\Transformers\Transformer;
10
11
class Rosetta
12
{
13
    /**
14
     * @param string $class
15
     * @param array  $config
16
     *
17
     * @throws ManagerException
18
     *
19
     * @return Connector
20
     */
21
    public static function connector($class, $config = [])
22
    {
23
        if (!str_contains($class, '\\')) {
24
            $class = '\\Ghc\\Rosetta\\Connectors\\'.studly_case($class);
25
        }
26
27
        if (!class_exists($class)) {
28
            throw new ManagerException("Connector class '$class' does not exists");
29
        }
30
31
        return new $class($config);
32
    }
33
34
    /**
35
     * @param Connector  $connector
36
     * @param string     $method
37
     * @param string     $uri
38
     * @param mixed|null $data
39
     * @param array      $options
40
     *
41
     * @return Request
42
     */
43
    public static function connectorRequest(Connector $connector, $method, $uri, $data = null, $options = [])
44
    {
45
        return new Request($connector, $method, $uri, $data, $options);
46
    }
47
48
    /**
49
     * @param string     $class
50
     * @param null|mixed $data
51
     * @param array      $config
52
     *
53
     * @throws ManagerException
54
     *
55
     * @return Message
56
     */
57 View Code Duplication
    public static function message($class, $data = null, $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        if (!str_contains($class, '\\')) {
60
            $class = '\\Ghc\\Rosetta\\Messages\\'.studly_case($class);
61
        }
62
63
        if (!class_exists($class)) {
64
            throw new ManagerException("Message class '$class' does not exists");
65
        }
66
67
        return new $class($data, $config);
68
    }
69
70
    /**
71
     * @param string $class
72
     * @param array  $config
73
     *
74
     * @throws ManagerException
75
     *
76
     * @return Message
77
     */
78
    public static function transformer($class, $config = [])
79
    {
80
        if (!str_contains($class, '\\')) {
81
            $class = '\\Ghc\\Rosetta\\Transformers\\'.studly_case($class);
82
        }
83
84
        if (!class_exists($class)) {
85
            throw new ManagerException("Transformer class '$class' does not exists");
86
        }
87
88
        return new $class($config);
89
    }
90
91
    /**
92
     * @param array|mixed               $data
93
     * @param Transformer|Transformer[] $transformers
94
     *
95
     * @return Item
96
     */
97
    public static function item($data, $transformers = null)
98
    {
99
        return new Item($data, $transformers);
100
    }
101
102
    /**
103
     * @param array|mixed               $data
104
     * @param Transformer|Transformer[] $transformers
105
     *
106
     * @return Item
107
     */
108
    public static function collection($data, $transformers = null)
109
    {
110
        return new Collection($data, $transformers);
111
    }
112
113
    /**
114
     * @param string $class
115
     *
116
     * @throws ManagerException
117
     *
118
     * @return
119
     */
120
    public static function pipe($class)
121
    {
122
        if (!str_contains($class, '\\')) {
123
            $class = '\\Ghc\\Rosetta\\Pipes\\'.studly_case($class);
124
        }
125
126
        if (!class_exists($class)) {
127
            throw new ManagerException("Pipe class '$class' does not exists");
128
        }
129
130
        return new $class();
131
    }
132
133
    /**
134
     * @param string     $class
135
     * @param null|mixed $data
136
     * @param array      $config
137
     *
138
     * @throws ManagerException
139
     *
140
     * @return
141
     */
142 View Code Duplication
    public static function matcher($class, $data = null, $config = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        if (!str_contains($class, '\\')) {
145
            $class = '\\Ghc\\Rosetta\\Matchers\\'.studly_case($class);
146
        }
147
148
        if (!class_exists($class)) {
149
            throw new ManagerException("Matcher class '$class' does not exists");
150
        }
151
152
        return new $class($data, $config);
153
    }
154
155
    /**
156
     * @param array $config
157
     *
158
     * @return Pipeline
159
     */
160
    public static function pipeline($config = [])
161
    {
162
        return new Pipeline($config);
163
    }
164
165
    /**
166
     * @param Message $input
167
     * @param Message $output
168
     *
169
     * @return Message
170
     */
171
    public static function transformMessage($input, $output)
172
    {
173
        return $output->fromArray($input->toArray());
174
    }
175
}
176