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