/** * * * @param MusicFile $file * @return int */ public function getVotes(MusicFile $file) : \int { $this->init(); return $this->votes[$file->getHash()] ?? 0; }
public function testPosition() { $file1 = new MusicFile("path2", $this->app); $file2 = new MusicFile("path1", $this->app); $file3 = new MusicFile("path3", $this->app); $this->app['caches'] = ['file' => $this->getCache([$file1->getHash(), $file2->getHash(), $file3->getHash()], [$file1->getHash() => 1, $file2->getHash() => 2, $file3->getHash() => 3])]; $this->assertEquals(0, $this->service->getPosition($file1)); $this->assertEquals(1, $this->service->getPosition($file2)); $this->assertEquals(2, $this->service->getPosition($file3)); }
/** * * @return MusicFile[string] */ private function getFromDirectories() : array { if (empty($this->app['config']['music']['extensions'])) { throw new \RuntimeException("File extensions are not set."); } if (empty($this->app['config']['music']['directories'])) { throw new \RuntimeException("Music directories are not set"); } $regex = "#(" . str_replace('.', '\\.', implode("|", $this->app['config']['music']['extensions'])) . ")\$#"; $files = []; foreach ($this->app['config']['music']['directories'] as $directory) { $path = realpath($directory); if ($path === false) { throw new \RuntimeException("Directory {$directory} could not be read."); } $dirIterator = new \RecursiveDirectoryIterator($path); $iterator = new \RegexIterator(new \RecursiveIteratorIterator($dirIterator), $regex, \RecursiveRegexIterator::MATCH); /** @var \SplFileInfo $directoryEntry */ foreach ($iterator as $directoryEntry) { $file = new MusicFile($directoryEntry->getPathname()); $files[$file->getHash()] = $file; } } return $files; }