bbrothers /
Muzzle
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Muzzle; |
||||
| 4 | |||||
| 5 | use BadMethodCallException; |
||||
| 6 | use Exception; |
||||
| 7 | use GuzzleHttp\ClientInterface; |
||||
| 8 | use Muzzle\Messages\Transaction; |
||||
| 9 | use Psr\Http\Message\RequestInterface; |
||||
| 10 | use Psr\Http\Message\ResponseInterface; |
||||
| 11 | |||||
| 12 | /** |
||||
| 13 | * @method MuzzleBuilder connect(string $uri = '/') |
||||
| 14 | * @method MuzzleBuilder delete(string $uri = '/') |
||||
| 15 | * @method MuzzleBuilder get(string $uri = '/') |
||||
| 16 | * @method MuzzleBuilder head(string $uri = '/') |
||||
| 17 | * @method MuzzleBuilder options(string $uri = '/') |
||||
| 18 | * @method MuzzleBuilder patch(string $uri = '/') |
||||
| 19 | * @method MuzzleBuilder post(string $uri = '/') |
||||
| 20 | * @method MuzzleBuilder put(string $uri = '/') |
||||
| 21 | * @method MuzzleBuilder trace(string $uri = '/') |
||||
| 22 | */ |
||||
| 23 | class MuzzleBuilder |
||||
| 24 | { |
||||
| 25 | |||||
| 26 | /** |
||||
| 27 | * @var array |
||||
| 28 | */ |
||||
| 29 | private $options = []; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * @var Transactions |
||||
| 33 | */ |
||||
| 34 | private $expectations; |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * @var callable[] |
||||
| 38 | */ |
||||
| 39 | private $middleware = []; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * @var RequestBuilder |
||||
| 43 | */ |
||||
| 44 | private $building; |
||||
| 45 | |||||
| 46 | public function __construct(Transactions $expectations = null) |
||||
| 47 | { |
||||
| 48 | |||||
| 49 | $this->expectations = $expectations ?: new Transactions; |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | public static function create(Transactions $transactions = null) : MuzzleBuilder |
||||
| 53 | { |
||||
| 54 | |||||
| 55 | return new static($transactions); |
||||
| 56 | } |
||||
| 57 | |||||
| 58 | public function withOptions(array $options) : MuzzleBuilder |
||||
| 59 | { |
||||
| 60 | |||||
| 61 | $this->options = array_merge($this->options, $options); |
||||
| 62 | |||||
| 63 | return $this; |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | public function withMiddleware(callable ...$middleware) : MuzzleBuilder |
||||
| 67 | { |
||||
| 68 | |||||
| 69 | $this->middleware = array_merge($this->middleware, $middleware); |
||||
| 70 | |||||
| 71 | return $this; |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | /** |
||||
| 75 | * @param RequestInterface|RequestBuilder $request |
||||
| 76 | * @param ResponseInterface|ResponseBuilder|Exception $response |
||||
| 77 | * @return MuzzleBuilder |
||||
| 78 | */ |
||||
| 79 | public function enqueue($request, $response) : self |
||||
| 80 | { |
||||
| 81 | |||||
| 82 | $transaction = new Transaction; |
||||
| 83 | $transaction->setRequest($request instanceof RequestBuilder ? $request->build() : $request); |
||||
| 84 | $transaction->setResponseOrError($response instanceof ResponseBuilder ? $response->build() : $response); |
||||
| 85 | |||||
| 86 | $this->expectations->push($transaction); |
||||
| 87 | |||||
| 88 | return $this; |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | public function buildRequest(HttpMethod $method, string $uri = '/') : MuzzleBuilder |
||||
| 92 | { |
||||
| 93 | |||||
| 94 | $builder = new RequestBuilder($method, $uri); |
||||
| 95 | if ($this->building) { |
||||
| 96 | $this->enqueue($this->building, $this->building->reply()); |
||||
| 97 | } |
||||
| 98 | $this->building = $builder; |
||||
| 99 | |||||
| 100 | return $this; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | public function build(array $options = []) : Muzzle |
||||
| 104 | { |
||||
| 105 | |||||
| 106 | if ($this->building) { |
||||
| 107 | $this->enqueue($this->building, $this->building->reply()); |
||||
| 108 | $this->building = null; |
||||
| 109 | } |
||||
| 110 | $this->withOptions($options); |
||||
| 111 | |||||
| 112 | |||||
| 113 | return Muzzle::make($options) |
||||
| 114 | ->append(...$this->expectations) |
||||
| 115 | ->addMiddleware(...$this->middleware); |
||||
| 116 | } |
||||
| 117 | |||||
| 118 | public function replace(array $options = []) : Muzzle |
||||
| 119 | { |
||||
| 120 | |||||
| 121 | $muzzle = $this->build($options); |
||||
| 122 | |||||
| 123 | if (function_exists('app') or function_exists(__NAMESPACE__ . '\\app')) { |
||||
|
0 ignored issues
–
show
|
|||||
| 124 | app()->extend(ClientInterface::class, function ($guzzle) use ($muzzle) { |
||||
| 125 | |||||
| 126 | return $muzzle->updateConfig($guzzle->getConfig()); |
||||
| 127 | }); |
||||
| 128 | } |
||||
| 129 | |||||
| 130 | return $muzzle; |
||||
| 131 | } |
||||
| 132 | |||||
| 133 | private function builder() : RequestBuilder |
||||
| 134 | { |
||||
| 135 | |||||
| 136 | if (! $this->building) { |
||||
| 137 | $this->building = new RequestBuilder; |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | return $this->building; |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | public function setMethod(HttpMethod $method) : MuzzleBuilder |
||||
| 144 | { |
||||
| 145 | |||||
| 146 | $this->builder()->setMethod($method); |
||||
| 147 | |||||
| 148 | return $this; |
||||
| 149 | } |
||||
| 150 | |||||
| 151 | public function setUri(?string $uri) : MuzzleBuilder |
||||
| 152 | { |
||||
| 153 | |||||
| 154 | $this->builder()->setUri($uri); |
||||
| 155 | |||||
| 156 | return $this; |
||||
| 157 | } |
||||
| 158 | |||||
| 159 | public function setHeaders(array $headers = []) : MuzzleBuilder |
||||
| 160 | { |
||||
| 161 | |||||
| 162 | $this->builder()->setHeaders($headers); |
||||
| 163 | |||||
| 164 | return $this; |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | public function setBody($body) : MuzzleBuilder |
||||
| 168 | { |
||||
| 169 | |||||
| 170 | $this->builder()->setBody($body); |
||||
| 171 | |||||
| 172 | return $this; |
||||
| 173 | } |
||||
| 174 | |||||
| 175 | public function setQuery(array $query = []) : MuzzleBuilder |
||||
| 176 | { |
||||
| 177 | |||||
| 178 | $this->builder()->setQuery($query); |
||||
| 179 | |||||
| 180 | return $this; |
||||
| 181 | } |
||||
| 182 | |||||
| 183 | public function replyWith($reply = null) : MuzzleBuilder |
||||
| 184 | { |
||||
| 185 | |||||
| 186 | $this->builder()->replyWith($reply); |
||||
| 187 | |||||
| 188 | return $this; |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | public function __call($method, $parameters) |
||||
| 192 | { |
||||
| 193 | |||||
| 194 | if (HttpMethod::isValid($method)) { |
||||
| 195 | return $this->buildRequest(new HttpMethod($method), ...$parameters); |
||||
|
0 ignored issues
–
show
$parameters is expanded, but the parameter $uri of Muzzle\MuzzleBuilder::buildRequest() does not expect variable arguments.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 196 | } |
||||
| 197 | |||||
| 198 | throw new BadMethodCallException(sprintf('The method %s is not defined.', $method)); |
||||
| 199 | } |
||||
| 200 | } |
||||
| 201 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.