Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
9 / 9
Container
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
7 / 7
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 get
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 define_services
n/a
0 / 0
1
n/a
0 / 0
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
14declare(strict_types=1);
15
16namespace RtCamp\GoogleLogin;
17
18use RtCamp\GoogleLogin\Interfaces\Container as ContainerInterface;
19use Pimple\Container as PimpleContainer;
20use InvalidArgumentException;
21use RtCamp\GoogleLogin\Modules\Assets;
22use RtCamp\GoogleLogin\Modules\Login;
23use RtCamp\GoogleLogin\Modules\Settings;
24use RtCamp\GoogleLogin\Utils\GoogleClient;
25use RtCamp\GoogleLogin\Modules\Shortcode;
26
27/**
28 * Class Container
29 *
30 * @package RtCamp\GoogleLogin
31 */
32class Container implements ContainerInterface {
33    /**
34     * Pimple container.
35     *
36     * @var PimpleContainer
37     */
38    public $container;
39
40    /**
41     * Container constructor.
42     *
43     * @param PimpleContainer $container Pimple Container.
44     */
45    public function __construct( PimpleContainer $container ) {
46        $this->container = $container;
47    }
48
49    /**
50     * Get the service object.
51     *
52     * @param string $service Service object in need.
53     *
54     * @return object
55     *
56     * @throws InvalidArgumentException Exception for invalid service.
57     */
58    public function get( string $service ) {
59        if ( ! in_array( $service, $this->container->keys() ) ) {
60            /* translators: %$s is replaced with requested service name. */
61            throw new InvalidArgumentException( sprintf( __( 'Invalid Service %s Passed to the container', 'login-with-google' ), $service ) );
62        }
63
64        return $this->container[ $service ];
65    }
66
67    /**
68     * Define common services in container.
69     *
70     * All the module specific services will be defined inside
71     * respective module's container.
72     *
73     * @codeCoverageIgnore
74     *
75     * @return void
76     */
77    public function define_services(): void {
78        /**
79         * Define Settings service to add settings page and retrieve setting values.
80         *
81         * @param PimpleContainer $c Pimple container object.
82         *
83         * @return Settings
84         */
85        $this->container['settings'] = function( PimpleContainer $c ) {
86            return new Settings();
87        };
88
89        /**
90         * Define the login flow service.
91         *
92         * @param PimpleContainer $c Pimple container object.
93         *
94         * @return Login
95         */
96        $this->container['login_flow'] = function( PimpleContainer $c ) {
97            return new Login( $c['gh_client'], $c['settings'] );
98        };
99
100        /**
101         * Define a service for Google OAuth client.
102         *
103         * @param PimpleContainer $c Pimple container instance.
104         *
105         * @return GoogleClient
106         */
107        $this->container['gh_client'] = function ( PimpleContainer $c ) {
108            $settings = $c['settings'];
109
110            return new GoogleClient(
111                [
112                    'client_id'     => $settings->client_id,
113                    'client_secret' => $settings->client_secret,
114                    'redirect_uri'  => wp_login_url(),
115                ]
116            );
117        };
118
119        /**
120         * Define Assets service to add styles or script.
121         *
122         * @param PimpleContainer $c Pimple container object.
123         *
124         * @return Assets
125         */
126        $this->container['assets'] = function ( PimpleContainer $c ) {
127            return new Assets();
128        };
129
130        /**
131         * Define Shortcode service to register shortcode for google login.
132         *
133         * @param PimpleContainer $c Pimple container object.
134         *
135         * @return Shortcode
136         */
137        $this->container['shortcode'] = function ( PimpleContainer $c ) {
138            return new Shortcode( $c['gh_client'], $c['assets'] );
139        };
140
141        /**
142         * Define any additional services.
143         *
144         * @param ContainerInterface $container Container object.
145         *
146         * @since 1.0.0
147         */
148        do_action( 'rtcamp.gh_login_services', $this );
149
150    }
151}