Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ortix's avatar

Help with if/else logical hell

I'm breaking my head over the following problem.

I have a form with a title field and alternative titles field.

Normally a user would fill in the title and it would get added to a list in a yaml file. However, sometimes the alternative titles field needs to be filled in (which takes precedence over the titles field, so it's an override). In this case all these titles (comma separated) will be added to the yaml file instead. There is also a checkbox above the alternative titles which indicates whether the original title should be added at all to the config.

The hell part comes when the user also wants to update the form. Then we have to take into consideration which titles we need to remove from the list. There are so many things that can happen that I've lost track on how to handle this.

I have a writer class which handles the i/o and parsing of the YAML file. It will only add or delete a title if it's present in the decoded array.

Here is some quick code:

public function store()
{
    $data = Input::all();
    $show = Show::create($data);
    $this->handleTitles($data, $show);
    return Redirect::route('shows.index');
}

public function update($id)
{
    $show = Show::findOrFail($id);
    $data = Input::all();
    $this->handleTitles($data, $show);
    $show->update($data);
    return Redirect::route('shows.show', $show->slug);
}

private function handleTitles($titles, $show)
{
    // We check if there is a difference between the titles on the model
    // and the titles we get passed through. If we are creating new then
    // the difference between the titles on the model and the titles from
    // the field is 0 (they are the same). In case we are updating we check
    // if there is an actual difference and only then do we delete and add
    $oldTitles = $show->flexget_titles;
    if (empty($oldTitles)) {
        // this happens when we are creating a new item
    } elseif (!empty(array_diff((array)$titles, $oldTitles))) {
        Flexget::config(Config::get('flexget.config_path'))->deleteShows($show->flexget_titles)->save();
    }
    Flexget::config(Config::get('flexget.config_path'))->addShows($titles)->save();
}

I hope this is not too much mumbo jumbo and someone will be able to help me out with this logic spaghetti.

0 likes
0 replies

Please or to participate in this conversation.