1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\Recommendation;
9
10class BatchProcessor
11{
12	private $store;
13	private $engines;
14
15	function __construct(Store\StoreInterface $store, EngineSet $engines)
16	{
17		$this->store = $store;
18		$this->engines = $engines;
19	}
20
21	function process()
22	{
23		$inputs = $this->store->getInputs();
24
25		foreach ($this->combined($inputs) as $entry) {
26			list($set, $engine, $input) = $entry;
27
28			foreach ($engine->generate($input) as $rec) {
29				if ($rec instanceof Recommendation && ! $this->store->isReceived($input, $rec)) {
30					$set->add($rec);
31				}
32			}
33
34			if (count($set) > 0) {
35				$this->store->store($input, $set);
36			}
37		}
38
39		$this->store->terminate();
40	}
41
42	private function combined($inputs)
43	{
44		$generator = $this->engines->getGenerator();
45		foreach ($inputs as $input) {
46			$current = $generator->current();
47			$generator->next();
48			if (! $current) {
49				return;
50			}
51
52			list($set, $engine) = $current;
53
54			yield [$set, $engine, $input];
55		}
56	}
57}
58