Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.30% |
36 / 37 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Http | |
97.30% |
36 / 37 |
|
88.89% |
8 / 9 |
25 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| setClient | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setUsername | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| setPassword | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| client | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResultResponse | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| hasClient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
7.05 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Auth; |
| 15 | |
| 16 | use Pop\Http\Client; |
| 17 | use Pop\Http\Auth; |
| 18 | |
| 19 | /** |
| 20 | * Http auth class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Auth |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2025 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 4.0.2 |
| 28 | */ |
| 29 | class Http extends AbstractAuth |
| 30 | { |
| 31 | |
| 32 | use AdapterUserTrait; |
| 33 | |
| 34 | /** |
| 35 | * Auth client |
| 36 | * @var Client |
| 37 | */ |
| 38 | protected $client = null; |
| 39 | |
| 40 | /** |
| 41 | * Constructor |
| 42 | * |
| 43 | * Instantiate the Http auth adapter object |
| 44 | * |
| 45 | */ |
| 46 | public function __construct() |
| 47 | { |
| 48 | $arguments = func_get_args(); |
| 49 | |
| 50 | foreach ($arguments as $argument) { |
| 51 | if ($argument instanceof Client) { |
| 52 | $this->setClient($argument); |
| 53 | } else if (($argument instanceof Auth) && ($this->client !== null)) { |
| 54 | $this->client->setAuth($argument); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Set client |
| 61 | * |
| 62 | * @param Client $client |
| 63 | * @return Http |
| 64 | */ |
| 65 | public function setClient(Client $client): Http |
| 66 | { |
| 67 | $this->client = $client; |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Set the username |
| 73 | * |
| 74 | * @param string $username |
| 75 | * @return Http |
| 76 | */ |
| 77 | public function setUsername(string $username): Http |
| 78 | { |
| 79 | parent::setUsername($username); |
| 80 | |
| 81 | if ($this->client !== null) { |
| 82 | if ($this->client->hasAuth()) { |
| 83 | $this->client->getAuth()->setUsername($username); |
| 84 | } else { |
| 85 | $this->client->addData($this->usernameField, $username); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Set the password |
| 94 | * |
| 95 | * @param string $password |
| 96 | * @return Http |
| 97 | */ |
| 98 | public function setPassword(string $password): Http |
| 99 | { |
| 100 | parent::setPassword($password); |
| 101 | |
| 102 | if ($this->client !== null) { |
| 103 | if ($this->client->hasAuth()) { |
| 104 | $this->client->getAuth()->setPassword($password); |
| 105 | } else { |
| 106 | $this->client->addData($this->passwordField, $password); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get client |
| 115 | * |
| 116 | * @return Client |
| 117 | */ |
| 118 | public function getClient(): Client |
| 119 | { |
| 120 | return $this->client; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get client (alias method) |
| 125 | * |
| 126 | * @return Client |
| 127 | */ |
| 128 | public function client(): Client |
| 129 | { |
| 130 | return $this->client; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get result response |
| 135 | * |
| 136 | * @return mixed |
| 137 | */ |
| 138 | public function getResultResponse(): mixed |
| 139 | { |
| 140 | $resultResponse = null; |
| 141 | if (($this->client->hasResponse()) && ($this->client->getResponse()->hasBody())) { |
| 142 | $resultResponse = $this->client->getResponse()->getParsedResponse(); |
| 143 | } |
| 144 | return $resultResponse; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | /** |
| 149 | * Has client |
| 150 | * |
| 151 | * @return bool |
| 152 | */ |
| 153 | public function hasClient(): bool |
| 154 | { |
| 155 | return ($this->client !== null); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Method to authenticate |
| 160 | * |
| 161 | * @param ?string $username |
| 162 | * @param ?string $password |
| 163 | * @return int |
| 164 | */ |
| 165 | public function authenticate(?string $username = null, ?string $password = null): int |
| 166 | { |
| 167 | if ($username !== null) { |
| 168 | $this->setUsername($username); |
| 169 | } |
| 170 | if ($password !== null) { |
| 171 | $this->setPassword($password); |
| 172 | } |
| 173 | |
| 174 | $this->client->send(); |
| 175 | $this->result = (int)(($this->client->hasResponse()) && ($this->client->getResponse()->isSuccess() == 200)); |
| 176 | |
| 177 | $response = $this->getResultResponse(); |
| 178 | |
| 179 | if (!empty($response) && is_array($response) && isset($response[$this->usernameField])) { |
| 180 | $this->user = $response; |
| 181 | } |
| 182 | |
| 183 | return $this->result; |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |