Completed
Push — master ( 6b77c2...0b4fa8 )
by Andrii
04:43
created

WordPressApi   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 112
ccs 0
cts 75
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getPage() 0 29 6
A getList() 0 24 3
A getClient() 0 18 2
A setUrl() 0 4 1
A setLogin() 0 4 1
A setPassword() 0 4 1
1
<?php
2
/**
3
 * Yii2 Pages Module
4
 *
5
 * @link      https://github.com/hiqdev/yii2-module-pages
6
 * @package   yii2-module-pages
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\modules\pages\storage;
12
13
use hiqdev\yii2\modules\pages\interfaces\StorageInterface;
14
use hiqdev\yii2\modules\pages\models\PagesList;
15
use Yii;
16
use hiqdev\yii2\modules\pages\models\AbstractPage;
17
use hiqdev\yii2\modules\pages\models\RenderedPage;
18
use Vnn\WpApiClient\Auth\WpBasicAuth;
19
use Vnn\WpApiClient\Http\GuzzleAdapter;
20
use Vnn\WpApiClient\WpClient;
21
use GuzzleHttp\Client as GuzzleClient;
22
use yii\base\BaseObject;
23
use yii\di\Instance;
24
use yii\helpers\Url;
25
26
/**
27
 * Class WordPressApi provides interaction with WordPress REST Api.
28
 *
29
 * The configuration in di-container should be like this:
30
 *
31
 * ```
32
 * 'WordpressApi' => [
33
 *      'class'     => full name of this class,
34
 *      'url'       => url to deployed wordpress,
35
 *      'login'     => admin login,
36
 *      'password'  => admin password,
37
 *  ]
38
 * ```
39
 * @package hiqdev\yii2\modules\pages\storage
40
 */
41
class WordPressApi extends BaseObject implements StorageInterface
42
{
43
    /** @var WpClient */
44
    private $client;
45
46
    /** @var string */
47
    private $url;
48
49
    /** @var string */
50
    private $login;
51
52
    /** @var string */
53
    private $password;
54
55
    public function getPage(string $pageName): ?AbstractPage
56
    {
57
        $language = \Yii::$app->language;
58
        $pageData = $this->getClient()->posts()->get(null, [
59
            'slug'  => $pageName,
60
        ]);
61
62
        if (empty($pageData)) {
63
            return null;
64
        }
65
66
        $pageData = $pageData[0];
67
        $translatedPage = $pageData['translation'][$language];
68
        if ($translatedPage && $language !== $pageData['lang']) {
69
            Yii::$app->response->redirect(Url::to('/pages/' . $translatedPage));
70
        }
71
        if (!$translatedPage && $language !== $pageData['lang']) {
72
            $canonical = Url::to($pageData['lang'] . '/pages/' . $pageData['slug'], true);
73
        }
74
75
        return Yii::createObject([
76
            'class' => RenderedPage::class,
77
            'title' => $pageData['title']['rendered'],
78
            'text' => $pageData['content']['rendered'],
79
            'keywords' => $pageData['seo']['keywords'],
80
            'description' => $pageData['seo']['description'],
81
            'canonical' => $canonical ?? null
82
        ]);
83
    }
84
85
    public function getList(): ?PagesList
86
    {
87
        $listData = $this->getClient()->posts()->get(null, [
88
            'lang'  => \Yii::$app->language,
89
        ]);
90
91
        if (empty($listData)) {
92
            return null;
93
        }
94
95
        $pages = [];
96
        foreach ($listData as $pageData) {
97
            $pages[] = (Yii::createObject([
98
                'class' => RenderedPage::class,
99
                'title' => $pageData['title']['rendered'],
100
                'text' => $pageData['excerpt']['rendered'],
101
                'slug' => $pageData['slug'],
102
                'featuredImageUrl' => $pageData['featured_image_url'],
103
                'url' => Url::to('/pages/' . $pageData['slug'])
104
            ]));
105
        }
106
107
        return Yii::createObject(PagesList::class, [ $pages ]);
108
    }
109
110
    private function getClient(): WpClient
111
    {
112
        if (!$this->client) {
113
            $this->client = Yii::$container->get(WpClient::class, [
114
                Yii::$container->get(GuzzleAdapter::class, [
115
                    Instance::of(GuzzleClient::class)
116
                ]),
117
                $this->url
118
            ]);
119
120
            $this->client->setCredentials(Yii::$container->get(WpBasicAuth::class, [
121
                $this->login,
122
                $this->password
123
            ]));
124
        }
125
126
        return $this->client;
127
    }
128
129
    /**
130
     * @param string $url
131
     */
132
    public function setUrl(string $url): void
133
    {
134
        $this->url = $url;
135
    }
136
137
    /**
138
     * @param string $login
139
     */
140
    public function setLogin(string $login): void
141
    {
142
        $this->login = $login;
143
    }
144
145
    /**
146
     * @param string $password
147
     */
148
    public function setPassword(string $password): void
149
    {
150
        $this->password = $password;
151
    }
152
}
153