Completed
Push — master ( cfd235...bd5dca )
by Dmitry
02:31
created

PackageController::actionSearch()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace hiqdev\assetpackagist\controllers;
4
5
use hiqdev\assetpackagist\exceptions\CorruptedPackageException;
6
use hiqdev\assetpackagist\exceptions\PackageNotExistsException;
7
use yii\web\Controller;
8
use Exception;
9
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
10
use hiqdev\assetpackagist\exceptions\UpdateRateLimitException;
11
use hiqdev\assetpackagist\models\AssetPackage;
12
use Yii;
13
use yii\filters\VerbFilter;
14
15
class PackageController extends Controller
16
{
17
    public function behaviors()
18
    {
19
        return [
20
            'access' => [
21
                'class' => VerbFilter::class,
22
                'actions' => [
23
                    'update' => ['post'],
24
                ],
25
            ],
26
        ];
27
    }
28
29
    public function actionUpdate()
30
    {
31
        session_write_close();
32
        $query = Yii::$app->request->post('query');
33
        $package = $this->getAssetPackage($query);
34
35
        try {
36
            if (!$package->canBeUpdated()) {
37
                throw new UpdateRateLimitException();
38
            }
39
40
            Yii::createObject(PackageUpdateCommand::class, [$package])->run();
41
        } catch (UpdateRateLimitException $exception) {
42
            Yii::$app->session->addFlash('rate-limited', true);
43
        } catch (PackageNotExistsException $e) {
44
            return $this->renderPartial('not-found', ['package' => $package]);
45
        } catch (CorruptedPackageException $e) {
46
            return $this->renderPartial('fetch-error', ['package' => $package, 'exception' => $e]);
47
        } catch (Exception $e) {
48
            Yii::error([
49
                'UNKNOWN error during ' . $package->getFullName() . ' update',
50
                get_class($e),
51
                $e->getMessage(),
52
            ], __METHOD__);
53
54
55
56
            return $this->renderPartial('fetch-error', ['package' => $package]);
57
        }
58
        $package->load();
59
60
        return $this->renderPartial('details', ['package' => $package]);
61
    }
62
63
    public function actionSearch($query)
64
    {
65
        try {
66
            $package = $this->getAssetPackage($query);
67
            $params = ['package' => $package, 'query' => $query, 'forceUpdate' => false];
68
69
            if ($package->canAutoUpdate()) {
70
                $params['forceUpdate'] = true;
71
            }
72
        } catch (Exception $e) {
73
            return $this->render('wrong-name', compact('query'));
74
        }
75
76
        return $this->render('search', $params);
77
    }
78
79
    /**
80
     * @param string $query
81
     * @return AssetPackage
82
     */
83
    private static function getAssetPackage($query)
84
    {
85
        $filtredQuery = trim($query);
86
        $package = AssetPackage::fromFullName($filtredQuery);
87
        $package->load();
88
89
        return $package;
90
    }
91
}
92