Skip to content

Commit 4b8656d

Browse files
authored
Merge pull request #23 from armanist/master
Major refactoring
2 parents feae4ec + bdaad84 commit 4b8656d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1241
-747
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# Quantum PHP Framework (project)
22

3-
- [Website](https://quantum.softberg.org)
4-
- [Blog](http://blog.softberg.org/category/quantum-php-framework/)
5-
63
[![Build Status](https://travis-ci.org/softberg/quantum-php-core.svg?branch=master)](https://travis-ci.org/softberg/quantum-php-core)
74
[![GitHub](https://img.shields.io/github/license/softberg/quantum-php-core)](https://github.com/softberg/quantum-php-core/blob/master/LICENSE)
85
[![Scrutinizer code quality](https://shields.cdn.bka.li/scrutinizer/quality/g/softberg/quantum-php-core)](https://scrutinizer-ci.com/g/softberg/quantum-php-core)

base/Commands/DemoCommand.php

Lines changed: 81 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.3.0
12+
* @since 2.5.0
1313
*/
1414

1515
namespace Base\Commands;
1616

17+
use Symfony\Component\Console\Output\NullOutput;
18+
use Symfony\Component\Console\Input\ArrayInput;
1719
use Quantum\Libraries\Storage\FileSystem;
20+
use Bluemmb\Faker\PicsumPhotosProvider;
1821
use Quantum\Console\QtCommand;
22+
use WW\Faker\Provider\Picture;
1923
use Quantum\Di\Di;
2024
use Faker\Factory;
21-
use Symfony\Component\Console\Input\ArrayInput;
22-
use Symfony\Component\Console\Output\NullOutput;
2325

2426

2527
/**
@@ -33,92 +35,128 @@ class DemoCommand extends QtCommand
3335
* Command name
3436
* @var string
3537
*/
36-
protected $name = 'demo:post';
37-
protected $faker;
38+
protected $name = 'install:demo';
39+
3840
/**
3941
* Command description
4042
* @var string
4143
*/
42-
protected $description = 'Generates posts.php and users.php files';
44+
protected $description = 'Generates demo users and posts';
4345

4446
/**
4547
* Command help text
4648
* @var string
4749
*/
48-
protected $help = 'The command will generate new posts.php and users.php files';
50+
protected $help = 'The command will create 2 new files (users.php and posts.php) and will generate records';
51+
52+
/**
53+
* @var \Faker\Generator
54+
*/
55+
protected $faker;
56+
57+
/**
58+
* How many posts to create
59+
*/
60+
const POST_COUNT = 6;
61+
62+
/**
63+
* Default password for generated users
64+
*/
65+
const DEFAULT_PASSWORD = 'password';
66+
67+
/**
68+
* Command name of create user
69+
*/
70+
const COMMAND_USER_CREATE = 'user:create';
4971

72+
/**
73+
* Command name of create post
74+
*/
75+
const COMMAND_POST_CREATE = 'post:create';
5076

77+
/**
78+
* Command constructor
79+
*/
5180
public function __construct()
5281
{
5382
parent::__construct();
5483
$this->faker = Factory::create();
84+
$this->faker->addProvider(new PicsumPhotosProvider($this->faker));
5585
}
5686

5787
/**
58-
* @return void
88+
* @throws \ReflectionException
5989
* @throws \Quantum\Exceptions\DiException
90+
* @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
91+
* @throws \Exception
6092
*/
6193
public function exec()
6294
{
63-
$userCommand = 'user:create';
64-
$postCommand = 'post:create';
65-
6695
$this->createFile('users');
6796

68-
$adminArguments = $this->createUser('admin');
69-
$guestArguments = $this->createUser();
70-
71-
$this->runCommand($adminArguments, $userCommand);
72-
$this->runCommand($guestArguments, $userCommand);
73-
97+
$adminArguments = $this->newUser('admin');
98+
$guestArguments = $this->newUser();
99+
100+
$this->runCommand(self::COMMAND_USER_CREATE, $adminArguments);
101+
$this->runCommand(self::COMMAND_USER_CREATE, $guestArguments);
74102

75103
$this->createFile('posts');
76104

77-
78-
for ($i = 1; $i <= 6; $i++) {
105+
for ($i = 1; $i <= self::POST_COUNT; $i++) {
79106
$postArguments = [
80-
'-t' => $this->faker->realText(30),
81-
'-d' => $this->faker->realText(),
82-
'-i' => $this->faker->imageUrl(480, 480, 'animals', true, 'cats'),
83-
'-a' => $adminArguments['-u'],
107+
'title' => $this->faker->realText(30),
108+
'description' => $this->faker->realText(500),
109+
'image' => $this->faker->imageUrl(640, 480, true, 0),
110+
'author' => $adminArguments['email'],
84111
];
85-
$this->runCommand($postArguments, $postCommand);
112+
113+
$this->runCommand(self::COMMAND_POST_CREATE, $postArguments);
86114
}
87-
115+
88116
}
89117

90-
protected function runCommand($arguments, $commandName){
118+
/**
119+
* Runs the external command
120+
* @throws \Exception
121+
*/
122+
protected function runCommand($commandName, $arguments)
123+
{
91124
$command = $this->getApplication()->find($commandName);
92-
$greetInput = new ArrayInput($arguments);
93-
$output = new NullOutput;
94-
$command->run($greetInput, $output);
125+
$command->run(new ArrayInput($arguments), new NullOutput);
95126
}
96127

128+
/**
129+
* Creates new repo file
130+
* @throws \ReflectionException
131+
* @throws \Symfony\Component\VarExporter\Exception\ExceptionInterface
132+
* @throws \Quantum\Exceptions\DiException
133+
*/
97134
protected function createFile($file)
98135
{
99136
$fs = Di::get(FileSystem::class);
100-
137+
101138
$repositoryDir = BASE_DIR . DS . 'base' . DS . 'repositories';
102-
$content = '<?php' . PHP_EOL . PHP_EOL . 'return ' . export([]) . ';';
139+
$content = '<?php' . PHP_EOL . PHP_EOL . 'return ' . export([]) . ';';
103140

104141
$fs->put($repositoryDir . DS . $file . '.php', $content);
105142

106143
$this->info(ucfirst($file) . ' successfully generated');
107144
}
108145

109-
110-
private function createUser($role = '')
146+
/**
147+
* Creates new user
148+
* @param string $role
149+
* @return array
150+
*/
151+
private function newUser(string $role = ''): array
111152
{
112-
$data =
113-
[
114-
'-f' => $this->faker->name(),
115-
'-l' => $this->faker->lastName(),
116-
'-r' => $role,
117-
'-u' => $this->faker->email(),
118-
'-p' => '123456',
119-
];
120-
121-
return $data;
153+
return [
154+
'firstname' => $this->faker->name(),
155+
'lastname' => $this->faker->lastName(),
156+
'role' => $role,
157+
'email' => $this->faker->email(),
158+
'password' => self::DEFAULT_PASSWORD,
159+
];
122160
}
123161

124162

base/Commands/PostCreateCommand.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.3.0
12+
* @since 2.5.0
1313
*/
1414

1515
namespace Base\Commands;
@@ -43,40 +43,39 @@ class PostCreateCommand extends QtCommand
4343
* Command help text
4444
* @var string
4545
*/
46-
protected $help = 'Add title and description to create a post';
46+
protected $help = 'Use the following format to create a post record:' . PHP_EOL . 'php qt post:create `Title` `Description` `[Image]` `[Author]`';
4747

4848
/**
49-
* Command options
50-
* @var array
49+
* Command arguments
50+
* @var \string[][]
5151
*/
52-
protected $options = [
53-
['title', 't', 'required', 'Post title'],
54-
['description', 'd', 'required', 'Post description'],
55-
['image', 'i', 'optional', 'Post image'],
56-
['author', 'a', 'optional', 'Post author'],
52+
protected $args = [
53+
['title', 'required', 'Post title'],
54+
['description', 'required', 'Post description'],
55+
['image', 'optional', 'Post image'],
56+
['author', 'optional', 'Post author'],
5757
];
5858

5959
/**
60-
* @return void
6160
* @throws \Quantum\Exceptions\DiException
61+
* @throws \ReflectionException
6262
*/
6363
public function exec()
6464
{
6565
$serviceFactory = Di::get(ServiceFactory::class);
66-
6766
$postService = $serviceFactory->get(PostService::class);
6867

6968
$post = [
70-
'title' => $this->getOption('title'),
71-
'content' => $this->getOption('description'),
72-
'author' => $this->getOption('author') ? $this->getOption('author') : 'anonymous@qt.com',
73-
'image' => $this->getOption('image') ? $this->getOption('image') : null,
69+
'title' => $this->getArgument('title'),
70+
'content' => $this->getArgument('description'),
71+
'author' => $this->getArgument('author') ?: 'anonymous@qt.com',
72+
'image' => $this->getArgument('image'),
7473
'updated_at' => date('m/d/Y H:i')
7574
];
7675

7776
$postService->addPost($post);
7877

79-
$this->info('Post create successfully');
78+
$this->info('Post created successfully');
8079
}
8180

8281
}

base/Commands/PostDeleteCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.3.0
12+
* @since 2.5.0
1313
*/
1414

1515
namespace Base\Commands;
@@ -43,19 +43,19 @@ class PostDeleteCommand extends QtCommand
4343
* Command help text
4444
* @var string
4545
*/
46-
protected $help = 'Provide ID to delete a post';
46+
protected $help = 'Use the following format to delete a post:' . PHP_EOL . 'php qt post:delete `Post Id`';
4747

4848
/**
4949
* Command arguments
50-
* @var array
50+
* @var \string[][]
5151
*/
5252
protected $args = [
5353
['id', 'required', 'Post ID'],
5454
];
5555

5656
/**
57-
* @return void
5857
* @throws \Quantum\Exceptions\DiException
58+
* @throws \ReflectionException
5959
*/
6060
public function exec()
6161
{

base/Commands/PostShowCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.3.0
12+
* @since 2.5.0
1313
*/
1414

1515
namespace Base\Commands;
@@ -38,13 +38,13 @@ class PostShowCommand extends QtCommand
3838
* Command description
3939
* @var string
4040
*/
41-
protected $description = 'Displays posts';
41+
protected $description = 'Displays all posts or a single post';
4242

4343
/**
4444
* Command help text
4545
* @var string
4646
*/
47-
protected $help = 'Displays all posts or single post on terminal';
47+
protected $help = 'Use the following format to display post(s):' . PHP_EOL . 'php qt post:show `[Post Id]`';
4848

4949
/**
5050
* Command arguments
@@ -55,8 +55,8 @@ class PostShowCommand extends QtCommand
5555
];
5656

5757
/**
58-
* @return void
5958
* @throws \Quantum\Exceptions\DiException
59+
* @throws \ReflectionException
6060
*/
6161
public function exec()
6262
{

base/Commands/PostUpdateCommand.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Arman Ag. <arman.ag@softberg.org>
1010
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
1111
* @link http://quantum.softberg.org/
12-
* @since 2.3.0
12+
* @since 2.5.0
1313
*/
1414

1515
namespace Base\Commands;
@@ -43,7 +43,7 @@ class PostUpdateCommand extends QtCommand
4343
* Command help text
4444
* @var string
4545
*/
46-
protected $help = 'Find post by ID and update the title or description of post';
46+
protected $help = 'Use the following format to update the post:' . PHP_EOL . 'php qt post:update `[Post Id]` -t `Title` -d `Description` [-i `Image`] [-a `Author`]';
4747

4848
/**
4949
* Command arguments
@@ -60,11 +60,13 @@ class PostUpdateCommand extends QtCommand
6060
protected $options = [
6161
['title', 't', 'required', 'Post title'],
6262
['description', 'd', 'required', 'Post description'],
63+
['image', 'i', 'optional', 'Post image'],
64+
['author', 'a', 'optional', 'Post author'],
6365
];
6466

6567
/**
66-
* @return void
6768
* @throws \Quantum\Exceptions\DiException
69+
* @throws \ReflectionException
6870
*/
6971
public function exec()
7072
{
@@ -74,14 +76,19 @@ public function exec()
7476

7577
$id = $this->getArgument('id');
7678

79+
$post = $postService->getPost($id);
80+
7781
$title = $this->getOption('title');
7882
$description = $this->getOption('description');
83+
$image = $this->getOption('image');
84+
$author = $this->getOption('author');
7985

8086
if($title && $description) {
8187
$post = [
8288
'title' => $title,
8389
'content' => $description,
84-
'author' => 'anonymous@qt.com',
90+
'image' => $image ?: $post['image'],
91+
'author' => $author ?: $post['author'],
8592
'updated_at' => date('m/d/Y H:i')
8693
];
8794

0 commit comments

Comments
 (0)