Completed
Push — master ( bdc0fb...7d20e3 )
by Dmitry
02:56
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 yii\web\Controller;
6
use Exception;
7
use hiqdev\assetpackagist\commands\PackageUpdateCommand;
8
use hiqdev\assetpackagist\exceptions\UpdateRateLimitException;
9
use hiqdev\assetpackagist\models\AssetPackage;
10
use Yii;
11
use yii\filters\VerbFilter;
12
13
class PackageController extends Controller
14
{
15
    public function behaviors()
16
    {
17
        return [
18
            'access' => [
19
                'class' => VerbFilter::class,
20
                'actions' => [
21
                    'update' => ['post'],
22
                ],
23
            ],
24
        ];
25
    }
26
27
    public function actionUpdate()
28
    {
29
        session_write_close();
30
        $query = Yii::$app->request->post('query');
31
        $package = $this->getAssetPackage($query);
32
33
        try {
34
            if (!$package->canBeUpdated()) {
35
                throw new UpdateRateLimitException();
36
            }
37
38
            Yii::createObject(PackageUpdateCommand::class, [$package])->run();
39
        } catch (UpdateRateLimitException $exception) {
40
            Yii::$app->session->addFlash('rate-limited', true);
41
        } catch (\Composer\Downloader\TransportException $e) {
42
            if (stripos($e->getMessage(), 'not found')) {
43
                Yii::warning('Failed to update ' . $package->getFullName() . ': ' . $e->getMessage(), __METHOD__);
44
45
                return $this->renderPartial('not-found', ['package' => $package]);
46
            }
47
48
            throw $e;
49
        } catch (\Composer\Repository\InvalidRepositoryException $e) {
50
            Yii::warning('Failed to update ' . $package->getFullName() . ': ' . $e->getMessage(), __METHOD__);
51
52
            return $this->renderPartial('fetch-error', ['package' => $package, 'exception' => $e]);
53
        } catch (Exception $e) {
54
            Yii::error([
55
                'UNKNOWN error during ' . $package->getFullName() . ' update',
56
                get_class($e),
57
                $e->getMessage(),
58
            ], __METHOD__);
59
60
            return $this->renderPartial('fetch-error', ['package' => $package, 'exception' => $e]);
61
        }
62
63
        $package->load();
64
65
        return $this->renderPartial('details', ['package' => $package]);
66
    }
67
68
    public function actionSearch($query)
69
    {
70
        try {
71
            $package = $this->getAssetPackage($query);
72
            $params = ['package' => $package, 'query' => $query, 'forceUpdate' => false];
73
74
            if ($package->canAutoUpdate()) {
75
                $params['forceUpdate'] = true;
76
            }
77
        } catch (Exception $e) {
78
            return $this->render('wrong-name', compact('query'));
79
        }
80
81
        return $this->render('search', $params);
82
    }
83
84
    /**
85
     * @param string $query
86
     * @return AssetPackage
87
     */
88
    private static function getAssetPackage($query)
89
    {
90
        $filtredQuery = trim($query);
91
        $package = AssetPackage::fromFullName($filtredQuery);
92
        $package->load();
93
94
        return $package;
95
    }
96
}
97