Skip to content

Commit

Permalink
Add logic for BasicSource
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 14, 2024
1 parent 82f09b0 commit 37d317b
Show file tree
Hide file tree
Showing 11 changed files with 573 additions and 6 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
on:
push:
branches: [ 'master' ]
pull_request:
branches: [ 'master' ]

permissions:
contents: read

jobs:
run:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php-versions: [ '5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ]

name: Run Unit Test on PHP ${{ matrix.php-versions }}

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}

- name: Check the PHP version
run: php -v

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: vendor/bin/phpunit --coverage-clover=coverage.clover

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $auth = new Authsum($source);
if ($auth->isValid($_POST))
{
/** @var \Acme\Models\User */
$user = $auth->getResult();
$user = $auth->getResult()->getField('user');

echo 'Welcome ' . $user->getName() . '!';
}
Expand Down
88 changes: 83 additions & 5 deletions src/Authsum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Rougin\Authsum;

use Rougin\Authsum\Source\SourceInterface;
use Rougin\Authsum\Source\WithPassword;
use Rougin\Authsum\Source\WithUsername;

/**
* @package Authsum
Expand All @@ -11,17 +13,31 @@
*/
class Authsum
{
/**
* @var string
*/
protected $password;

/**
* @var \Rougin\Authsum\Source\SourceInterface
*/
protected $source;

/**
* @var string
*/
protected $username;

/**
* @param \Rougin\Authsum\Source\SourceInterface $source
*/
public function __construct(SourceInterface $source)
{
$this->source = $source;

$this->setPasswordField('password');

$this->setUsernameField('email');
}

/**
Expand All @@ -35,6 +51,16 @@ public function getError()
return $this->source->getError();
}

/**
* Gets the password field.
*
* @return string
*/
public function getPasswordField()
{
return $this->password;
}

/**
* Returns the result after validation.
*
Expand All @@ -47,14 +73,13 @@ public function getResult()
}

/**
* Executes if the validation failed.
*
* @param \Rougin\Authsum\Error $error
* Gets the username field.
*
* @return void
* @return string
*/
protected function failed(Error $error)
public function getUsernameField()
{
return $this->username;
}

/**
Expand All @@ -66,6 +91,20 @@ protected function failed(Error $error)
*/
public function isValid($payload)
{
if ($this->source instanceof WithUsername)
{
$username = $payload[$this->getUsernameField()];

$this->source->setUsername($username);
}

if ($this->source instanceof WithPassword)
{
$password = $payload[$this->getPasswordField()];

$this->source->setPassword($password);
}

$valid = $this->source->isValid();

if ($valid)
Expand All @@ -80,6 +119,45 @@ public function isValid($payload)
return $valid;
}

/**
* Sets the password field.
*
* @param string $password
*
* @return self
*/
public function setPasswordField($password)
{
$this->password = $password;

return $this;
}

/**
* Sets the username field.
*
* @param string $username
*
* @return self
*/
public function setUsernameField($username)
{
$this->username = $username;

return $this;
}

/**
* Executes if the validation failed.
*
* @param \Rougin\Authsum\Error $error
*
* @return void
*/
protected function failed(Error $error)
{
}

/**
* Executes if the validation passed.
*
Expand Down
24 changes: 24 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,28 @@
*/
class Error
{
/**
* @var string|null
*/
protected $text = null;

/**
* @return string|null
*/
public function getText()
{
return $this->text;
}

/**
* @param string $text
*
* @return self
*/
public function setText($text)
{
$this->text = $text;

return $this;
}
}
71 changes: 71 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,82 @@

namespace Rougin\Authsum;

use UnexpectedValueException;

/**
* @package Authsum
*
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Result
{
/**
* @var array<string, mixed>
*/
protected $data = array();

/**
* @var string|null
*/
protected $text = null;

/**
* @return string|null
*/
public function getText()
{
return $this->text;
}

/**
* @param string $key
*
* @return mixed
*/
public function getField($key)
{
if (! $this->hasField($key))
{
$text = 'Field "' . $key . '" not found';

throw new UnexpectedValueException($text);
}

return $this->data[$key];
}

/**
* @param string $key
*
* @return boolean
*/
public function hasField($key)
{
return array_key_exists($key, $this->data);
}

/**
* @param string $key
* @param mixed $value
*
* @return self
*/
public function setField($key, $value)
{
$this->data[$key] = $value;

return $this;
}

/**
* @param string $text
*
* @return self
*/
public function setText($text)
{
$this->text = $text;

return $this;
}
}
56 changes: 56 additions & 0 deletions src/Source.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rougin\Authsum;

use Rougin\Authsum\Source\SourceInterface;
use UnexpectedValueException;

/**
* @package Authsum
*
* @author Rougin Gutib <rougingutib@gmail.com>
*/
abstract class Source implements SourceInterface
{
/**
* @var \Rougin\Authsum\Error|null
*/
protected $error = null;

/**
* @var \Rougin\Authsum\Result|null
*/
protected $result = null;

/**
* Returns the error after validation.
*
* @return \Rougin\Authsum\Error
* @throws \UnexpectedValueException
*/
public function getError()
{
if (! $this->error)
{
throw new UnexpectedValueException('Validation passed');
}

return $this->error;
}

/**
* Returns the result after validation.
*
* @return \Rougin\Authsum\Result
* @throws \UnexpectedValueException
*/
public function getResult()
{
if (! $this->result)
{
throw new UnexpectedValueException('Validation failed');
}

return $this->result;
}
}
Loading

0 comments on commit 37d317b

Please sign in to comment.