-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCouchDbCheck.php
More file actions
135 lines (114 loc) · 4.04 KB
/
Copy pathCouchDbCheck.php
File metadata and controls
135 lines (114 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace PhpSchool\CouchDb;
use Doctrine\CouchDB\CouchDBClient;
use Doctrine\CouchDB\HTTP\HTTPException;
use PhpSchool\PhpWorkshop\Check\ListenableCheckInterface;
use PhpSchool\PhpWorkshop\Event\CliExecuteEvent;
use PhpSchool\PhpWorkshop\Event\Event;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Result\Failure;
use PhpSchool\PhpWorkshop\Result\Success;
/**
* @package PhpSchool\SimpleMath\Check
* @author Aydin Hassan <aydin@hotmail.co.uk>
*/
class CouchDbCheck implements ListenableCheckInterface
{
/**
* @var string
*/
private static $studentDb = 'phpschool-student';
/**
* @var string
*/
private static $solutionDb = 'phpschool';
/**
* Return the check's name
*
* @return string
*/
public function getName()
{
return 'Couch DB Verification Check';
}
/**
* This returns the interface the exercise should implement
* when requiring this check
*
* @return string
*/
public function getExerciseInterface()
{
return CouchDbExerciseCheck::class;
}
/**
* @param EventDispatcher $eventDispatcher
*/
public function attach(EventDispatcher $eventDispatcher)
{
$studentClient = CouchDBClient::create(['dbname' => static::$studentDb]);
$solutionClient = CouchDBClient::create(['dbname' => static::$solutionDb]);
$studentClient->createDatabase($studentClient->getDatabase());
$solutionClient->createDatabase($solutionClient->getDatabase());
$eventDispatcher->listen('verify.start', function (Event $e) use ($studentClient, $solutionClient) {
$e->getParameter('exercise')->seed($studentClient);
$this->replicateDbFromStudentToSolution($studentClient, $solutionClient);
});
$eventDispatcher->listen('run.start', function (Event $e) use ($studentClient) {
$e->getParameter('exercise')->seed($studentClient);
});
$eventDispatcher->listen('cli.verify.reference-execute.pre', function (CliExecuteEvent $e) {
$e->prependArg('phpschool');
});
$eventDispatcher->listen(
['cli.verify.student-execute.pre', 'cli.run.student-execute.pre'],
function (CliExecuteEvent $e) {
$e->prependArg('phpschool-student');
}
);
$eventDispatcher->insertVerifier('verify.finish', function (Event $e) use ($studentClient) {
$verifyResult = $e->getParameter('exercise')->verify($studentClient);
if (false === $verifyResult) {
return Failure::fromNameAndReason($this->getName(), 'Database verification failed');
}
return Success::fromCheck($this);
});
$eventDispatcher->listen(
[
'cli.verify.reference-execute.fail',
'verify.finish',
'run.finish'
],
function (Event $e) use ($studentClient, $solutionClient) {
$studentClient->deleteDatabase(static::$studentDb);
$solutionClient->deleteDatabase(static::$solutionDb);
}
);
}
/**
* @param CouchDBClient $studentClient
* @param CouchDBClient $solutionClient
* @throws \Doctrine\CouchDB\HTTP\HTTPException
*/
private function replicateDbFromStudentToSolution(CouchDBClient $studentClient, CouchDBClient $solutionClient)
{
$response = $studentClient->allDocs();
if ($response->status !== 200) {
//should maybe throw an exception - but what should we print?
return;
}
foreach ($response->body['rows'] as $row) {
$doc = $row['doc'];
$data = array_filter($doc, function ($key) {
return !in_array($key, ['_id', '_rev']);
}, ARRAY_FILTER_USE_KEY);
try {
$solutionClient->putDocument(
$data,
$doc['_id']
);
} catch (HTTPException $e) {
}
}
}
}