Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Container | |
100.00% |
4 / 4 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
get | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
define_services | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | /** |
3 | * Class Container. |
4 | * |
5 | * This will be useful for creation of object. |
6 | * We are using Pimple DI Container, which will be |
7 | * useful for defining services and serves as service |
8 | * locator. |
9 | * |
10 | * @package RtCamp\GoogleLogin |
11 | * @since 1.0.0 |
12 | */ |
13 | |
14 | declare(strict_types=1); |
15 | |
16 | namespace RtCamp\GoogleLogin; |
17 | |
18 | use RtCamp\GoogleLogin\Interfaces\Container as ContainerInterface; |
19 | use Pimple\Container as PimpleContainer; |
20 | use InvalidArgumentException; |
21 | use RtCamp\GoogleLogin\Modules\Assets; |
22 | use RtCamp\GoogleLogin\Modules\Block; |
23 | use RtCamp\GoogleLogin\Modules\Login; |
24 | use RtCamp\GoogleLogin\Modules\OneTapLogin; |
25 | use RtCamp\GoogleLogin\Modules\Settings; |
26 | use RtCamp\GoogleLogin\Utils\Authenticator; |
27 | use RtCamp\GoogleLogin\Utils\GoogleClient; |
28 | use RtCamp\GoogleLogin\Modules\Shortcode; |
29 | use RtCamp\GoogleLogin\Utils\TokenVerifier; |
30 | |
31 | /** |
32 | * Class Container |
33 | * |
34 | * @package RtCamp\GoogleLogin |
35 | */ |
36 | class Container implements ContainerInterface { |
37 | /** |
38 | * Pimple container. |
39 | * |
40 | * @var PimpleContainer |
41 | */ |
42 | public $container; |
43 | |
44 | /** |
45 | * Container constructor. |
46 | * |
47 | * @param PimpleContainer $container Pimple Container. |
48 | */ |
49 | public function __construct( PimpleContainer $container ) { |
50 | $this->container = $container; |
51 | } |
52 | |
53 | /** |
54 | * Get the service object. |
55 | * |
56 | * @param string $service Service object in need. |
57 | * |
58 | * @return object |
59 | * |
60 | * @throws InvalidArgumentException Exception for invalid service. |
61 | */ |
62 | public function get( string $service ) { |
63 | if ( ! in_array( $service, $this->container->keys() ) ) { |
64 | /* translators: %$s is replaced with requested service name. */ |
65 | throw new InvalidArgumentException( sprintf( __( 'Invalid Service %s Passed to the container', 'login-with-google' ), $service ) ); |
66 | } |
67 | |
68 | return $this->container[ $service ]; |
69 | } |
70 | |
71 | /** |
72 | * Define common services in container. |
73 | * |
74 | * All the module specific services will be defined inside |
75 | * respective module's container. |
76 | * |
77 | * @codeCoverageIgnore |
78 | * |
79 | * @return void |
80 | */ |
81 | public function define_services(): void { |
82 | /** |
83 | * Define Settings service to add settings page and retrieve setting values. |
84 | * |
85 | * @param PimpleContainer $c Pimple container object. |
86 | * |
87 | * @return Settings |
88 | */ |
89 | $this->container['settings'] = function( PimpleContainer $c ) { |
90 | return new Settings(); |
91 | }; |
92 | |
93 | /** |
94 | * Define the login flow service. |
95 | * |
96 | * @param PimpleContainer $c Pimple container object. |
97 | * |
98 | * @return Login |
99 | */ |
100 | $this->container['login_flow'] = function( PimpleContainer $c ) { |
101 | return new Login( $c['gh_client'], $c['authenticator'] ); |
102 | }; |
103 | |
104 | /** |
105 | * Define a service for Google OAuth client. |
106 | * |
107 | * @param PimpleContainer $c Pimple container instance. |
108 | * |
109 | * @return GoogleClient |
110 | */ |
111 | $this->container['gh_client'] = function ( PimpleContainer $c ) { |
112 | $settings = $c['settings']; |
113 | |
114 | return new GoogleClient( |
115 | [ |
116 | 'client_id' => $settings->client_id, |
117 | 'client_secret' => $settings->client_secret, |
118 | 'redirect_uri' => wp_login_url(), |
119 | ] |
120 | ); |
121 | }; |
122 | |
123 | /** |
124 | * Define Assets service to add styles or script. |
125 | * |
126 | * @param PimpleContainer $c Pimple container object. |
127 | * |
128 | * @return Assets |
129 | */ |
130 | $this->container['assets'] = function ( PimpleContainer $c ) { |
131 | return new Assets(); |
132 | }; |
133 | |
134 | /** |
135 | * Define Shortcode service to register shortcode for google login. |
136 | * |
137 | * @param PimpleContainer $c Pimple container object. |
138 | * |
139 | * @return Shortcode |
140 | */ |
141 | $this->container['shortcode'] = function ( PimpleContainer $c ) { |
142 | return new Shortcode( $c['gh_client'], $c['assets'] ); |
143 | }; |
144 | |
145 | /** |
146 | * Define Token Verifier Service. |
147 | * |
148 | * Useful in verifying JWT Auth token. |
149 | * |
150 | * @param PimpleContainer $c Pimple container object. |
151 | * |
152 | * @return TokenVerifier |
153 | */ |
154 | $this->container['token_verifier'] = function ( PimpleContainer $c ) { |
155 | return new TokenVerifier( $c['settings'] ); |
156 | }; |
157 | |
158 | /** |
159 | * One Tap Login Service. |
160 | * |
161 | * @param PimpleContainer $c Pimple container object. |
162 | * |
163 | * @return OneTapLogin |
164 | */ |
165 | $this->container['one_tap_login'] = function ( PimpleContainer $c ) { |
166 | return new OneTapLogin( $c['settings'], $c['token_verifier'], $c['gh_client'], $c['authenticator'] ); |
167 | }; |
168 | |
169 | /** |
170 | * Authenticator utility. |
171 | * |
172 | * @param PimpleContainer $c Pimple container object. |
173 | * |
174 | * @return Authenticator |
175 | */ |
176 | $this->container['authenticator'] = function ( PimpleContainer $c ) { |
177 | return new Authenticator( $c['settings'] ); |
178 | }; |
179 | |
180 | /** |
181 | * Define Block service to add gutenberg block. |
182 | * |
183 | * @param PimpleContainer $c Pimple container object. |
184 | * |
185 | * @return Block |
186 | */ |
187 | $this->container['google_login_block'] = function ( PimpleContainer $c ) { |
188 | return new Block( $c['assets'], $c['gh_client'] ); |
189 | }; |
190 | |
191 | |
192 | /** |
193 | * Define any additional services. |
194 | * |
195 | * @param ContainerInterface $container Container object. |
196 | * |
197 | * @since 1.0.0 |
198 | */ |
199 | do_action( 'rtcamp.google_login_services', $this ); |
200 | } |
201 | } |