22
33namespace SystemCtl \Unit ;
44
5- use Symfony \Component \Process \Process ;
65use SystemCtl \Command \CommandDispatcherInterface ;
6+ use SystemCtl \Command \CommandInterface ;
77
8+ /**
9+ * Class AbstractUnit
10+ *
11+ * @package SystemCtl\Unit
12+ */
813abstract class AbstractUnit implements UnitInterface
914{
1015 /** @var string */
@@ -13,6 +18,11 @@ abstract class AbstractUnit implements UnitInterface
1318 /** @var CommandDispatcherInterface */
1419 protected $ commandDispatcher ;
1520
21+ /**
22+ * @var string
23+ */
24+ protected $ unitSuffix ;
25+
1626 /**
1727 * Create new service with given name
1828 *
@@ -57,60 +67,83 @@ public function getInstanceName(): ?string
5767 return $ instanceName ;
5868 }
5969
70+ /**
71+ * @return string
72+ */
73+ abstract protected function getUnitSuffix (): string ;
74+
75+ /**
76+ * @param array $commands
77+ *
78+ * @return CommandInterface
79+ */
80+ public function execute (...$ commands ): CommandInterface
81+ {
82+ $ commands [] = implode (
83+ '. ' ,
84+ [
85+ $ this ->name ,
86+ $ this ->getUnitSuffix (),
87+ ]
88+ );
89+
90+ return $ this ->commandDispatcher ->dispatch (...$ commands );
91+ }
92+
6093 /**
6194 * @return bool
6295 */
6396 public function start (): bool
6497 {
65- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
98+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
6699 }
67100
68101 /**
69102 * @return bool
70103 */
71104 public function stop (): bool
72105 {
73- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
106+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
74107 }
75108
76109 /**
77110 * @return bool
78111 */
79112 public function disable (): bool
80113 {
81- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
114+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
82115 }
83116
84117 /**
85118 * @return bool
86119 */
87120 public function reload (): bool
88121 {
89- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
122+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
90123 }
91124
92125 /**
93126 * @return bool
94127 */
95128 public function restart (): bool
96129 {
97- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
130+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
98131 }
99132
100133 /**
101134 * @return bool
102135 */
103136 public function enable (): bool
104137 {
105- return $ this ->commandDispatcher -> dispatch (__FUNCTION__ )->isSuccessful ();
138+ return $ this ->execute (__FUNCTION__ )->isSuccessful ();
106139 }
107140
108141 /**
109142 * @return bool
110143 */
111144 public function isEnabled (): bool
112145 {
113- $ output = $ this ->commandDispatcher -> dispatch ('is-enabled ' )->getOutput ();
146+ $ output = $ this ->execute ('is-enabled ' )->getOutput ();
114147
115148 return trim ($ output ) === 'enabled ' ;
116149 }
@@ -120,7 +153,7 @@ public function isEnabled(): bool
120153 */
121154 public function isActive (): bool
122155 {
123- $ output = $ this ->commandDispatcher -> dispatch ('is-active ' )->getOutput ();
156+ $ output = $ this ->execute ('is-active ' )->getOutput ();
124157
125158 return trim ($ output ) === 'active ' ;
126159 }
0 commit comments