I think uploading packs is more or less working. I nutted out a few bugs in a lot...
[rock.divinelegy.git] / Controllers / SimfileController.php
1 <?php
2
3 namespace Controllers;
4
5 use Controllers\IDivineController;
6 use Services\Http\IHttpResponse;
7 use Services\Uploads\IUploadManager;
8 use Services\IUserSession;
9 use Services\IZipParser;
10 use DataAccess\StepMania\ISimfileRepository;
11 use DataAccess\StepMania\IPackRepository;
12 use DataAccess\IFileRepository;
13 use Domain\Entities\StepMania\ISimfile;
14 use Domain\Entities\StepMania\IPack;
15
16 class SimfileController implements IDivineController
17 {
18 private $_simfileRepository;
19 private $_packRepository;
20 private $_fileRepository;
21 private $_response;
22 private $_uploadManager;
23 private $_userSession;
24 private $_zipParser;
25
26 public function __construct(
27 IHttpResponse $response,
28 IUploadManager $uploadManager,
29 ISimfileRepository $simfileRepository,
30 IPackRepository $packRepository,
31 IFileRepository $fileRepository,
32 IUserSession $userSession,
33 IZipParser $zipParser
34 ) {
35 $this->_response = $response;
36 $this->_uploadManager = $uploadManager;
37 $this->_simfileRepository = $simfileRepository;
38 $this->_packRepository = $packRepository;
39 $this->_fileRepository = $fileRepository;
40 $this->_userSession = $userSession;
41 $this->_zipParser = $zipParser;
42 }
43
44 public function indexAction() {
45 ;
46 }
47
48 // list simfiles
49 public function listAction()
50 {
51 /* @var $simfile Domain\Entities\StepMania\ISimfile */
52 $simfiles = $this->_simfileRepository->findRange(1, 10);
53 $returnArray = array();
54
55 foreach($simfiles as $simfile)
56 {
57 $singleSteps = array();
58 $doubleSteps = array();
59
60 foreach($simfile->getSteps() as $steps)
61 {
62 $stepDetails = array(
63 'artist' => $steps->getArtist()->getTag(),
64 'difficulty' => $steps->getDifficulty()->getITGName(),
65 'rating' => $steps->getRating()
66 );
67
68 if($steps->getMode()->getPrettyName() == 'Single')
69 {
70 $singleSteps[] = $stepDetails;
71 } else {
72 $doubleSteps[] = $stepDetails;
73 }
74 }
75
76 $returnArray[] = array(
77 'title' => $simfile->getTitle(),
78 'artist' => $simfile->getArtist()->getName(),
79 'steps' => array(
80 'single' => $singleSteps,
81 'double' => $doubleSteps
82 ),
83 'bgChanges' => $simfile->hasBgChanges() ? 'Yes' : 'No',
84 'fgChanges' => $simfile->hasFgChanges() ? 'Yes' : 'No',
85 'bpmChanges' => $simfile->hasBPMChanges() ? 'Yes' : 'No',
86 'banner' => $simfile->getBanner() ? 'files/banner/' . $simfile->getBanner()->getHash() : 'files/banner/default'
87 );
88 }
89
90 $this->_response->setHeader('Content-Type', 'application/json')
91 ->setBody(json_encode($returnArray))
92 ->sendResponse();
93 }
94
95 public function uploadAction()
96 {
97 //TODO: Put directory in config ?
98 $files = $this->_uploadManager->setFilesDirectory('../files')
99 ->setDestination('StepMania/')
100 ->process();
101
102 foreach($files as $file)
103 {
104 $zipParser = $this->_zipParser;
105 $zipParser->parse($file);
106
107 //save the actual zip in the db
108 $this->_fileRepository->save($file);
109
110 if($zipParser->isPack())
111 {
112 //XXX: Tricky! pack() uses packbuilder and so returns a new pack each time.
113 //I tried to be clever and call pack() multiple times thinking I was getting the same
114 //object. Should I cache it in zipparser?
115 $pack = $zipParser->pack();
116 $this->_fileRepository->save($pack->getBanner());
117 $this->_packRepository->save($pack);
118 }
119
120 foreach($zipParser->simfiles() as $simfile)
121 {
122 $banner = $simfile->getBanner() ? $this->_fileRepository->save($simfile->getBanner()) : null;
123 $simfileZip = $simfile->getSimfile() ? $this->_fileRepository->save($simfile->getSimfile()) : null;
124
125 if(isset($pack)) $simfile->addToPack($pack);
126 $this->_simfileRepository->save($simfile);
127 }
128 }
129 }
130 }