Created
August 30, 2019 09:14
-
-
Save kolomiec-valeriy/928283af1333a109b547174c0f64ab17 to your computer and use it in GitHub Desktop.
Assigning verses to emotions from csv files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controller; | |
use App\Entity\HeartTracker\Emotion; | |
use App\Entity\Position; | |
use App\Entity\Verse; | |
use Symfony\Component\Filesystem\Exception\FileNotFoundException; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\Routing\Annotation\Route; | |
class ImportVersesToEmotionsController extends Controller | |
{ | |
/** | |
* Assigning verses to emotions from csv files | |
* | |
* @Route("/verses-to-emotions", name="add_verses_to_emotions") | |
*/ | |
public function addedVersesToEmotions() | |
{ | |
$errors = [ | |
'verses' => [], | |
'emotions' => [], | |
]; | |
$em = $this->getDoctrine()->getManager(); | |
$emotionFiles = [ | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/1_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/2_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/3_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/4_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/5_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/6_7.csv', | |
// 'https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/7_7.csv', | |
]; | |
$allBooksFile = file_get_contents('https://soultime-tmp.s3-eu-west-1.amazonaws.com/dev/import/all_books.csv'); | |
if (!$allBooksFile) { | |
throw new FileNotFoundException('All books not found'); | |
} | |
$tmpExplode = explode(PHP_EOL, $allBooksFile); | |
$allBooks = []; | |
foreach ($tmpExplode as $key => $string) { | |
$string = explode(',', $string); | |
if (isset($string[1]) && !empty($string[1])) { | |
$allBooks[$string[0]] = $string[1]; | |
} | |
} | |
$emotions = []; | |
foreach ($emotionFiles as $link) { | |
$emotionFile = file_get_contents($link); | |
$tmpEmotionsExplode = explode(PHP_EOL, $emotionFile); | |
$key = 1; | |
foreach ($tmpEmotionsExplode as $string) { | |
$string = explode(',', $string); | |
if (isset($string[1]) && !empty($string[1])) { | |
if (!isset($emotions[$string[0]])) { | |
$key = 1; | |
} | |
if ((int)$string[3] < (int)$string[5]) { | |
while ((int)$string[3] <= (int)$string[5]) { | |
$emotions[$string[0]][$key][$allBooks[$string[1]]] = $string[2].':'.$string[3].','.$string[4].':'.$string[3]; | |
$string[3]++; | |
$key++; | |
} | |
} else { | |
$emotions[$string[0]][$key][$allBooks[$string[1]]] = $string[2].':'.$string[3].','.$string[4].':'.$string[5]; | |
$key++; | |
} | |
} | |
$string = ''; | |
} | |
// find verses | |
$allVersesToEmotion = []; | |
foreach ($emotions as $emotionId => $emotionVersesArray) { | |
foreach ($emotionVersesArray as $position => $verse) { | |
$verseSearchBook = array_keys($verse); | |
$searchVerses = explode(',', $verse[$verseSearchBook[0]]); | |
$verses = $em->getRepository(Verse::class)->findByParams( | |
$verseSearchBook[0], | |
$searchVerses[0], | |
$searchVerses[1] | |
); | |
if (count($verses)) { | |
$allVersesToEmotion[$position] = reset($verses); | |
} else { | |
$errors['verses'][] = [ | |
'book' => $verseSearchBook, | |
'verses' => $searchVerses, | |
'emotionId' => $emotionId, | |
]; | |
} | |
// reset variables | |
$verseSearchBook = []; | |
$searchVerses = []; | |
$verses = []; | |
} | |
// adding verses to emotion | |
/** @var Emotion $emotion */ | |
$emotion = $em->getRepository(Emotion::class)->find($emotionId); | |
foreach ($emotion->getVerses() as $emotionVerse) { | |
$emotion->removeVerse($emotionVerse); | |
$position = $this->getDoctrine()->getRepository(Position::class)->findOneBy([ | |
'verse' => $emotionVerse, | |
'emotion' => $emotion, | |
]); | |
if ($position) { | |
$em->remove($position); | |
} | |
$position = null; | |
} | |
$em->flush(); | |
foreach ($allVersesToEmotion as $pos => $verseForEmotion) { | |
if ($verseForEmotion instanceof Verse) { | |
$emotion->addVerse($verseForEmotion); | |
$position = new Position(); | |
$position->setVerse($verseForEmotion); | |
$position->setEmotion($emotion); | |
$position->setPosition($pos); | |
$em->persist($position); | |
} else { | |
$errors['emotions'][] = $emotion->getId(); | |
} | |
} | |
$em->flush(); | |
// reset variables | |
$allVersesToEmotion = []; | |
$emotion = null; | |
} | |
} | |
return $this->json($errors); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment