1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 
<?php
/**
 * Copyright (C) Marcelle Hövelmanns, art solution - All Rights Reserved
 *
 * @file   AuthService.php
 * @author Marcelle Hövelmanns
 * @site   http://www.artsolution.de
 * @date   28.10.17
 */

namespace AffiliconApiClient\Services;


use AffiliconApiClient\Client;
use AffiliconApiClient\Exceptions\AuthenticationFailed;

/**
 * Class Authentication
 *
 * @package AffiliconApiClient\Traits
 */
class AuthService
{
    protected $token;
    protected $username;
    protected $password;
    protected $route;

    /** @var  Client */
    protected $client;

    /**
     * AuthService constructor.
     * @param $client
     */
    public function __construct($client)
    {
        $this->client = $client;
    }

    public function isAuthenticated()
    {
        return !is_null($this->token);
    }

    public function member()
    {
        $this->route = $this->client
            ->config()
            ->get('routes.auth.member');

        return $this;
    }

    public function employee()
    {
        $this->route = $this->client
            ->config()
            ->get('routes.auth.employee');

        return $this;
    }

    public function anonymous()
    {
        $this->route = $this->client
            ->config()
            ->get('routes.auth.anonymous');

        return $this;
    }

    public function authenticate()
    {
        if ($this->isAuthenticated()) {
            return $this->getToken();
        }

        try {

            $meta = $this->client
                ->http()
                ->post($this->route)
                ->body();

        } catch (\Exception $e) {

            throw new AuthenticationFailed($e->getMessage(), $e->getCode());
        }

        if (!$meta || !$meta->token) {

            throw new AuthenticationFailed('token invalid', 403);

        }

        $this->client->http()->setHeaders([
            'Authorization' => "Bearer $meta->token"
        ]);

        $this->token = $meta->token;

        return $this;
    }

    public function setUserName($username)
    {
        $this->username = $username;
        return $this;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function setPassword($password)
    {
        $this->password = $password;
        return $this;
    }

    public function getToken()
    {
        return $this->token;
    }
}