Completed
Push — master ( 3b5f44...5ab887 )
by Владислав
02:49
created

DeCaptchaWikiMain::setText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace jumper423\decaptcha\core;
4
5
/**
6
 * Class DeCaptchaWikiMain.
7
 */
8
class DeCaptchaWikiMain extends DeCaptchaWiki
9
{
10
    protected $classes = [];
11
12
    public function __construct($class)
13
    {
14
        parent::__construct($class);
15
        $this->texts['slug_menu_desc'] = [
16
            'ru' => 'Описание',
17
            'en' => 'Description',
18
        ];
19
        $this->texts['readme_main_desc'] = [
20
            'ru' => 'Пакет создан для стандартизации всех сервисов по разгадыванию капч. 
21
            У каждого сервиса есть свои особенности и теперь Вам надо будет всего лишь взглянуть на документацию конкретного сервиса чтобы правильно всё сделать.
22
            Так же пакет покрывает всю функциональсть сервисов. Если же Вам будет чего-то нехватать или будут предложения, я буду только рад их услышать.',
23
            'en' => 'Package created to standardize all services for solving captcha.
24
            Each service has its own features and now You will have to look at the documentation for the specific service to do everything right. 
25
            The package covers the entire functionality services. If You will be something lacking or suggestions, I\'ll be glad to hear them.',
26
        ];
27
        $this->texts['slug_menu_services'] = [
28
            'ru' => 'Сервисы',
29
            'en' => 'Services',
30
        ];
31
        $this->texts['readme_main_services'] = [
32
            'ru' => 'Распознавание капч для всех популярных сервисов',
33
            'en' => 'Recognition captchas for all popular services',
34
        ];
35
        $this->texts['slug_menu_features'] = [
36
            'ru' => 'Особенности',
37
            'en' => 'Features',
38
        ];
39
        $this->texts['readme_main_features'] = [
40
            'ru' => [
41
                '+ Подходит для всех сервисов по распознаванию капч',
42
                '+ Можно легко добавить новый сервис, используя уже готовый движок',
43
                '+ Быстрая и интуительно понятная настройка',
44
                '+ Распознавание как по пути до файла, так и по ссылки',
45
                '+ ReCaptcha v2 без браузера',
46
                '+ Полная документация',
47
                '+ Покрыт тестами',
48
            ],
49
            'en' => [
50
                '+ Suitable for all recognition services captcha',
51
                '+ You can easily add a new service using the existing engine',
52
                '+ Intuitable fast and straightforward setup',
53
                '+ Recognition as the path to the file and links',
54
                '+ ReCaptcha v2 without a browser',
55
                '+ Full documentation',
56
                '+ Covered by tests',
57
            ],
58
        ];
59
    }
60
61
    public function addClass($class)
62
    {
63
        $this->classes[] = $class;
64
    }
65
66
    protected function viewHeader()
67
    {
68
        return 'DeCaptcha
69
================
70
[![Latest Stable Version](https://poser.pugx.org/jumper423/decaptcha/v/stable)](https://packagist.org/packages/jumper423/decaptcha)
71
[![Total Downloads](https://poser.pugx.org/jumper423/decaptcha/downloads)](https://packagist.org/packages/jumper423/decaptcha)
72
[![License](https://poser.pugx.org/jumper423/decaptcha/license)](https://packagist.org/packages/jumper423/decaptcha)
73
74
[![Build Status](https://travis-ci.org/jumper423/decaptcha.svg?branch=master)](https://travis-ci.org/jumper423/decaptcha)
75
[![Dependency Status](https://www.versioneye.com/user/projects/5849f365a662a5004c110a29/badge.svg?style=flat-square)](https://www.versioneye.com/user/projects/5849f365a662a5004c110a29)
76
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jumper423/decaptcha/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jumper423/decaptcha/?branch=master)
77
[![Build Status](https://scrutinizer-ci.com/g/jumper423/decaptcha/badges/build.png?b=master)](https://scrutinizer-ci.com/g/jumper423/decaptcha/build-status/master)
78
[![Code Climate](https://codeclimate.com/github/jumper423/decaptcha/badges/gpa.svg)](https://codeclimate.com/github/jumper423/decaptcha)
79
[![Issue Count](https://codeclimate.com/github/jumper423/decaptcha/badges/issue_count.svg)](https://codeclimate.com/github/jumper423/decaptcha)
80
[![codecov](https://codecov.io/gh/jumper423/decaptcha/branch/master/graph/badge.svg)](https://codecov.io/gh/jumper423/decaptcha)
81
[![HHVM Status](http://hhvm.h4cc.de/badge/jumper423/decaptcha.svg)](http://hhvm.h4cc.de/package/jumper423/decaptcha)
82
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/d485629c-1830-440d-82ab-a567bfa5ddc5/mini.png)](https://insight.sensiolabs.com/projects/d485629c-1830-440d-82ab-a567bfa5ddc5)
83
[![StyleCI](https://styleci.io/repos/75013766/shield?branch=master)](https://styleci.io/repos/75013766)'.PHP_EOL;
84
    }
85
86
    /**
87
     * @param string|array $name
88
     * @param string       $separator
89
     *
90
     * @return string|array
91
     */
92 View Code Duplication
    public function getText($name, $separator = '; ')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $getResult = function ($name, $texts) {
95
            if (is_array($name)) {
96
                $name = implode('_', $name);
97
            }
98
            if (!isset($texts[$name])) {
99
                return null;
100
            }
101
            if (is_array($texts[$name])) {
102
                if (isset($texts[$name][$this->lang])) {
103
                    return $texts[$name][$this->lang];
104
                }
105
106
                return array_values($texts[$name])[0];
107
            }
108
109
            return $texts[$name];
110
        };
111
        $result = $getResult($name, $this->texts);
112
        if (is_array($result)) {
113
            if ($separator) {
114
                $result = implode($separator, $result);
115
            }
116
        }
117
118
        return $result;
119
    }
120
121
    protected function viewMenu($master = '')
122
    {
123
        $str = "+ [{$this->getText(['slug', 'menu', 'another'])}](../{$master}docs/".$this->getFileName($this->lang == 'ru' ? 'en' : 'ru').')'.PHP_EOL;
124
        foreach ([
125
                     ['slug', 'menu', 'desc'],
126
                     ['slug', 'menu', 'features'],
127
                     ['slug', 'menu', 'services'],
128
                     ['install'],
129
                     ['example'],
130
                 ] as $anchor) {
131
            $str .= "+ [{$this->getText($anchor)}](#".implode('-', explode(' ', ($this->lang === 'en' ? mb_strtolower($this->getText($anchor)) : $this->getText($anchor)))).')'.PHP_EOL;
132
        }
133
134
        return $str;
135
    }
136
137
    public function viewServices($master = '')
138
    {
139
        $str = '';
140
        $tt = [];
141
        foreach ($this->classes as $class) {
142
            if (isset($tt[$class->getWiki($this->lang)->getFileName()])) {
143
                continue;
144
            }
145
            $tt[$class->getWiki($this->lang)->getFileName()] = true;
146
            $fromServiceObjectWiki = $class->getWiki($this->lang);
147
            $str .= "+ [{$fromServiceObjectWiki->getText(['service', 'name'])}](../{$master}docs/{$fromServiceObjectWiki->getFileName()})".PHP_EOL;
148
        }
149
150
        return $str;
151
    }
152
153
    public function view($master = '')
154
    {
155
        $str = $this->viewHeader().PHP_EOL;
156
157
        $str .= "###{$this->getText(['slug', 'menu'])}".PHP_EOL;
158
        $str .= $this->viewMenu($master).PHP_EOL.PHP_EOL;
159
160
        $str .= "###{$this->getText(['slug', 'menu', 'desc'])}".PHP_EOL;
161
        $str .= $this->getText(['readme', 'main', 'desc']).PHP_EOL.PHP_EOL;
162
163
        $str .= "###{$this->getText(['slug', 'menu', 'features'])}".PHP_EOL;
164
        $str .= $this->getText(['readme', 'main', 'features'], PHP_EOL).PHP_EOL.PHP_EOL;
165
166
        $str .= "###{$this->getText(['slug', 'menu', 'services'])}".PHP_EOL;
167
        $str .= "{$this->getText(['readme', 'main', 'services'])}".PHP_EOL.PHP_EOL;
168
        $str .= "{$this->viewServices($master)}".PHP_EOL.PHP_EOL;
169
170
        $str .= "###{$this->getText(['install'])}".PHP_EOL;
171
        $str .= "{$this->viewInstall()}".PHP_EOL.PHP_EOL;
172
        $str .= "###{$this->getText(['example'])}".PHP_EOL;
173
        $str .= "{$this->viewExamples()}".PHP_EOL.PHP_EOL;
174
175
        return $str;
176
    }
177
178
    public function getFileName($lang = null)
179
    {
180
        if (is_null($lang)) {
181
            $lang = $this->lang;
182
        }
183
184
        return 'README-'.$lang.'.md';
185
    }
186
187
    public function save()
188
    {
189
        file_put_contents(__DIR__.'/../../docs/'.$this->getFileName(), $this->view());
190
        file_put_contents(__DIR__.'/../../README.md', $this->view('master/'));
191
    }
192
}
193