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
1515namespace Base \Commands ;
1616
17+ use Symfony \Component \Console \Output \NullOutput ;
18+ use Symfony \Component \Console \Input \ArrayInput ;
1719use Quantum \Libraries \Storage \FileSystem ;
20+ use Bluemmb \Faker \PicsumPhotosProvider ;
1821use Quantum \Console \QtCommand ;
22+ use WW \Faker \Provider \Picture ;
1923use Quantum \Di \Di ;
2024use 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
0 commit comments