1414use Spork \Batch \Strategy \ChunkStrategy ;
1515use Spork \Batch \Strategy \StrategyInterface ;
1616use Spork \Exception \UnexpectedTypeException ;
17+ use Spork \AbstractJob ;
1718use Spork \ProcessManager ;
1819
19- class BatchJob
20+ class BatchJob extends AbstractJob
2021{
21- private $ manager ;
22- private $ data ;
23- private $ strategy ;
24- private $ name ;
25- private $ callback ;
22+ protected $ strategy ;
2623
2724 public function __construct (ProcessManager $ manager , $ data = null , StrategyInterface $ strategy = null )
2825 {
29- $ this ->manager = $ manager ;
30- $ this ->data = $ data ;
26+ parent ::__construct ($ manager , $ data );
3127 $ this ->strategy = $ strategy ?: new ChunkStrategy ();
32- $ this ->name = '<anonymous> ' ;
33- }
34-
35- public function setName ($ name )
36- {
37- $ this ->name = $ name ;
38-
39- return $ this ;
4028 }
4129
4230 public function setStrategy (StrategyInterface $ strategy )
@@ -46,31 +34,13 @@ public function setStrategy(StrategyInterface $strategy)
4634 return $ this ;
4735 }
4836
49- public function setData ($ data )
50- {
51- $ this ->data = $ data ;
52-
53- return $ this ;
54- }
55-
56- public function setCallback ($ callback )
57- {
58- if (!is_callable ($ callback )) {
59- throw new UnexpectedTypeException ($ callback , 'callable ' );
60- }
61-
62- $ this ->callback = $ callback ;
63-
64- return $ this ;
65- }
66-
6737 public function execute ($ callback = null )
6838 {
6939 if (null !== $ callback ) {
7040 $ this ->setCallback ($ callback );
7141 }
7242
73- return $ this ->manager ->fork ($ this )->setName ($ this ->name . ' batch ' );
43+ return $ this ->manager ->fork ($ this )->setName ($ this ->name . ' batch ' );
7444 }
7545
7646 /**
@@ -84,8 +54,7 @@ public function __invoke()
8454 foreach ($ this ->strategy ->createBatches ($ this ->data ) as $ index => $ batch ) {
8555 $ forks [] = $ this ->manager
8656 ->fork ($ this ->strategy ->createRunner ($ batch , $ this ->callback ))
87- ->setName (sprintf ('%s batch #%d ' , $ this ->name , $ index ))
88- ;
57+ ->setName (sprintf ('%s batch #%d ' , $ this ->name , $ index ));
8958 }
9059
9160 // block until all forks have exited
0 commit comments