Completed
Pull Request — develop (#13)
by Stephen
04:18
created

Controller::connect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
ccs 7
cts 7
cp 1
crap 2
1
<?php
2
3
namespace Spinen\QuickBooks\Http\Controllers;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Contracts\View\Factory as ViewFactory;
7
use Illuminate\Http\Request;
8
use Illuminate\Routing\Controller as LaravelController;
9
use Illuminate\Routing\Redirector;
10
use Spinen\QuickBooks\Client as QuickBooks;
11
12
/**
13
 * Class Controller
14
 *
15
 * @package Spinen\QuickBooks
16
 */
17
class Controller extends LaravelController
18
{
19
    // NOTE: When using constructor injection for QuickBooks, there is a race issue with the boot order of the app
20
21
    /**
22
     * Form to connect/disconnect user to QuickBooks
23
     *
24
     * If the user has a valid OAuth, then give form to disconnect, otherwise link to connect it
25
     *
26
     * @param QuickBooks $quickbooks
27
     * @param ViewFactory $view_factory
28
     *
29
     * @return \Illuminate\Contracts\View\View|\Illuminate\View\View
0 ignored issues
show
Bug introduced by
The type Illuminate\View\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     * @throws \QuickBooksOnline\API\Exception\SdkException
31
     * @throws \QuickBooksOnline\API\Exception\ServiceException
32
     */
33 2
    public function connect(QuickBooks $quickbooks, ViewFactory $view_factory)
34
    {
35
        // Give view to remove token if user already linked account
36 2
        if ($quickbooks->hasValidRefreshToken()) {
37 1
            return $view_factory->make('quickbooks::disconnect')
38 1
                                ->with('company', $quickbooks->getDataService()
39 1
                                                             ->getCompanyInfo());
40
        }
41
42
        // Give view to link account
43 1
        return $view_factory->make('quickbooks::connect')
44 1
                            ->with('authorization_uri', $quickbooks->authorizationUri());
45
    }
46
47
    /**
48
     * Removes the token
49
     *
50
     * @param Redirector $redirector
51
     * @param QuickBooks $quickbooks
52
     *
53
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
54
     * @throws \Exception
55
     */
56
    public function disconnect(Redirector $redirector, QuickBooks $quickbooks)
57
    {
58
        $quickbooks->deleteToken();
59
60
        // TODO: Figure out where to put this in session & remove Facade
61
        Alert::success('Disconnected from QuickBooks')
0 ignored issues
show
Bug introduced by
The type Spinen\QuickBooks\Http\Controllers\Alert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
             ->flash();
63
64
        return $redirector->back();
65
    }
66
67
    /**
68
     * Accept the code from QuickBooks to request token
69
     *
70
     * Once a user approves linking account, then QuickBooks sends back
71
     * a code which can be converted to an OAuth token.
72
     *
73
     * @param Redirector $redirector
74
     * @param Request $request
75
     * @param QuickBooks $quickbooks
76
     * @param UrlGenerator $url_generator
77
     *
78
     * @return \Illuminate\Http\RedirectResponse
79
     * @throws \QuickBooksOnline\API\Exception\SdkException
80
     * @throws \QuickBooksOnline\API\Exception\ServiceException
81
     */
82
    public function token(Redirector $redirector, Request $request, QuickBooks $quickbooks, UrlGenerator $url_generator)
83
    {
84
        // TODO: Deal with exceptions
85
        $quickbooks->exchangeCodeForToken($request->get('code'), $request->get('realmId'));
86
87
        return $redirector->intended($url_generator->route('quickbooks.connect'))
88
                          ->with('success', 'Connected to QuickBooks');
89
    }
90
}
91