title: Symfony login user improve controller quality
body: <p>Welcome,
  I wrote login system, it works but i think it is so bad code quality. Too many if. I want separate this code, cut down if amount and create some 'error login message' code. I need some tip to rewrite this controller method. It made my code more testable and pretty.
  Thanks for every answer</p>
  
  <pre><code> /**
   * @Route("/login", name="login")
   */
  public function loginAction(Request $request)
  {
      $session = new Session();
      if ($request-&gt;getMethod() == 'POST') {
          $userData = $request-&gt;request-&gt;all();
          $newUser = new User();
  
          $foundUser = $this-&gt;getDoctrine()-&gt;getRepository('CmsUserBundle:User')-&gt;loadUsername($userData['username']);
  
          if ($foundUser != null) {
              $encodedPassword = md5($this-&gt;get('security.password_encoder')-&gt;encodePassword($newUser, $foundUser-&gt;getSalt()));
              if ($encodedPassword == $foundUser-&gt;getPassword() &amp;&amp; $foundUser-&gt;getIsActive() == true) {
                  $role =  $this-&gt;getDoctrine()-&gt;getRepository('CmsUserBundle:Role')-&gt;findBy(array('id' =&gt; $foundUser-&gt;getRoles()))[0];
  
                  if($role-&gt;getIsActive() === TRUE) {
                      $token = new UsernamePasswordToken($foundUser, $foundUser-&gt;getPassword(), 'default', array($role-&gt;getRole()) );
                      $this-&gt;get('security.token_storage')-&gt;setToken($token);
                      $session-&gt;getFlashBag()-&gt;add('success', 'Pomyślnie zalogowano');
                      return $this-&gt;redirect($this-&gt;generateUrl('index'));
                  }
                  $session-&gt;getFlashBag()-&gt;add('success', 'Role access can recent disbaled');
              } else {
                  $session-&gt;getFlashBag()-&gt;add('success', 'Invalid password or login');
              }
          } else {
              $session-&gt;getFlashBag()-&gt;add('success', 'Bad data');
          }
      }
      return $this-&gt;render('CmsUserBundle:Default:login.html.twig',
          array(
              'sessions' =&gt; $session-&gt;getFlashBag()-&gt;get('success'),
          )
      );
  }
  </code></pre>