1<?php
2/*
3 *
4 * Copyright 2018 gRPC authors.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20namespace Grpc;
21
22/**
23 * Represents an interceptor that intercept RPC invocations before call starts.
24 * There is one proposal related to the argument $deserialize under the review.
25 * The proposal link is https://github.com/grpc/proposal/pull/86.
26 * This is an EXPERIMENTAL API.
27 */
28class Interceptor
29{
30    public function interceptUnaryUnary(
31        $method,
32        $argument,
33        $deserialize,
34        array $metadata = [],
35        array $options = [],
36        $continuation
37    ) {
38        return $continuation($method, $argument, $deserialize, $metadata, $options);
39    }
40
41    public function interceptStreamUnary(
42        $method,
43        $deserialize,
44        array $metadata = [],
45        array $options = [],
46        $continuation
47    ) {
48        return $continuation($method, $deserialize, $metadata, $options);
49    }
50
51    public function interceptUnaryStream(
52        $method,
53        $argument,
54        $deserialize,
55        array $metadata = [],
56        array $options = [],
57        $continuation
58    ) {
59        return $continuation($method, $argument, $deserialize, $metadata, $options);
60    }
61
62    public function interceptStreamStream(
63        $method,
64        $deserialize,
65        array $metadata = [],
66        array $options = [],
67        $continuation
68    ) {
69        return $continuation($method, $deserialize, $metadata, $options);
70    }
71
72    /**
73     * Intercept the methods with Channel
74     *
75     * @param Channel|InterceptorChannel $channel An already created Channel or InterceptorChannel object (optional)
76     * @param Interceptor|Interceptor[] $interceptors interceptors to be added
77     *
78     * @return InterceptorChannel
79     */
80    public static function intercept($channel, $interceptors)
81    {
82        if (is_array($interceptors)) {
83            for ($i = count($interceptors) - 1; $i >= 0; $i--) {
84                $channel = new Internal\InterceptorChannel($channel, $interceptors[$i]);
85            }
86        } else {
87            $channel =  new Internal\InterceptorChannel($channel, $interceptors);
88        }
89        return $channel;
90    }
91}
92
93