1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spinen\QuickBooks\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
7
|
|
|
use Illuminate\Contracts\Session\Session; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Routing\Redirector; |
10
|
|
|
use Spinen\QuickBooks\Client; |
11
|
|
|
use Spinen\QuickBooks\Client as QuickBooks; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Filter |
15
|
|
|
* |
16
|
|
|
* @package Spinen\QuickBooks |
17
|
|
|
*/ |
18
|
|
|
class Filter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The QuickBooks client instance. |
22
|
|
|
* |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
protected $quickbooks; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The redirector instance. |
29
|
|
|
* |
30
|
|
|
* @var Redirector |
31
|
|
|
*/ |
32
|
|
|
protected $redirector; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The session instance. |
36
|
|
|
* |
37
|
|
|
* @var Session |
38
|
|
|
*/ |
39
|
|
|
protected $session; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The UrlGenerator instance. |
43
|
|
|
* |
44
|
|
|
* @var UrlGenerator |
45
|
|
|
*/ |
46
|
|
|
protected $url_generator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Create a new QuickBooks filter middleware instance. |
50
|
|
|
* |
51
|
|
|
* @param QuickBooks $quickbooks |
52
|
|
|
* @param Redirector $redirector |
53
|
|
|
* @param Session $session |
54
|
|
|
* @param UrlGenerator $url_generator |
55
|
|
|
*/ |
56
|
3 |
|
public function __construct( |
57
|
|
|
QuickBooks $quickbooks, |
58
|
|
|
Redirector $redirector, |
59
|
|
|
Session $session, |
60
|
|
|
UrlGenerator $url_generator |
61
|
|
|
) { |
62
|
3 |
|
$this->quickbooks = $quickbooks; |
63
|
3 |
|
$this->redirector = $redirector; |
64
|
3 |
|
$this->session = $session; |
65
|
3 |
|
$this->url_generator = $url_generator; |
66
|
3 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle an incoming request. |
70
|
|
|
* |
71
|
|
|
* @param Request $request Request |
72
|
|
|
* @param Closure $next Closure |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
2 |
|
public function handle(Request $request, Closure $next) |
77
|
|
|
{ |
78
|
2 |
|
if (!$this->quickbooks->hasValidRefreshToken()) { |
79
|
|
|
// Set intended route, so that after linking account, user is put where they were going |
80
|
1 |
|
$this->session->put('url.intended', $this->url_generator->to($request->path())); |
81
|
|
|
|
82
|
1 |
|
return $this->redirector->route('quickbooks.connect'); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $next($request); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|