zammad/zammad

View on GitHub
app/models/chat.rb

Summary

Maintainability
D
2 days
Test Coverage

File chat.rb has 459 lines of code (exceeds 250 allowed). Consider refactoring.
Open

class Chat < ApplicationModel
  include ChecksHtmlSanitized

  has_many :sessions, dependent: :destroy

Severity: Minor
Found in app/models/chat.rb - About 7 hrs to fix

    Class Chat has 24 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class Chat < ApplicationModel
      include ChecksHtmlSanitized
    
      has_many :sessions, dependent: :destroy
    
    
    Severity: Minor
    Found in app/models/chat.rb - About 2 hrs to fix

      Method customer_state has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

        def customer_state(session_id = nil)
          return { state: 'chat_disabled' } if !Setting.get('chat')
      
          # reconnect
          if session_id
      Severity: Minor
      Found in app/models/chat.rb - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Method customer_state has 36 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def customer_state(session_id = nil)
          return { state: 'chat_disabled' } if !Setting.get('chat')
      
          # reconnect
          if session_id
      Severity: Minor
      Found in app/models/chat.rb - About 1 hr to fix

        Method agent_state has 34 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def self.agent_state(user_id)
            return { state: 'chat_disabled' } if !Setting.get('chat')
        
            current_user = User.lookup(id: user_id)
            return { error: "No such user with id: #{user_id}" } if !current_user
        Severity: Minor
        Found in app/models/chat.rb - About 1 hr to fix

          Method broadcast_agent_state_update has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
          Open

            def self.broadcast_agent_state_update(chat_ids, ignore_user_id = nil)
          
              # send broadcast to agents
              Chat::Agent.where('active = ? OR updated_at > ?', true, 8.hours.ago).each do |item|
                next if item.updated_by_id == ignore_user_id
          Severity: Minor
          Found in app/models/chat.rb - About 55 mins to fix

          Cognitive Complexity

          Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

          A method's cognitive complexity is based on a few simple rules:

          • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
          • Code is considered more complex for each "break in the linear flow of the code"
          • Code is considered more complex when "flow breaking structures are nested"

          Further reading

          Avoid deeply nested control flow statements.
          Open

                      if session
                        return {
                          state:   'reconnect',
                          session: session,
                          agent:   user,
          Severity: Major
          Found in app/models/chat.rb - About 45 mins to fix

            Method agent_state has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
            Open

              def self.agent_state(user_id)
                return { state: 'chat_disabled' } if !Setting.get('chat')
            
                current_user = User.lookup(id: user_id)
                return { error: "No such user with id: #{user_id}" } if !current_user
            Severity: Minor
            Found in app/models/chat.rb - About 45 mins to fix

            Cognitive Complexity

            Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

            A method's cognitive complexity is based on a few simple rules:

            • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
            • Code is considered more complex for each "break in the linear flow of the code"
            • Code is considered more complex when "flow breaking structures are nested"

            Further reading

            Avoid too many return statements within this method.
            Open

                  return {
                    state: 'no_seats_available',
                    queue: waiting_count,
                  }
            Severity: Major
            Found in app/models/chat.rb - About 30 mins to fix

              Method agent_active_chat_ids has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                def self.agent_active_chat_ids(user)
                  return [] if user.preferences[:chat].blank?
                  return [] if user.preferences[:chat][:active].blank?
              
                  chat_ids = []
              Severity: Minor
              Found in app/models/chat.rb - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method blocked_ip? has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                def blocked_ip?(ip)
                  return false if ip.blank?
                  return false if block_ip.blank?
              
                  ips = block_ip.split(';')
              Severity: Minor
              Found in app/models/chat.rb - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method active_agent_count has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                def self.active_agent_count(chat_ids, diff = 2.minutes)
                  count = 0
                  Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
                    user = User.lookup(id: record.updated_by_id)
                    next if !user
              Severity: Minor
              Found in app/models/chat.rb - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method active_agents has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                def self.active_agents(chat_ids, diff = 2.minutes)
                  users = []
                  Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
                    user = User.lookup(id: record.updated_by_id)
                    next if !user
              Severity: Minor
              Found in app/models/chat.rb - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Method available_agents_with_concurrent has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
              Open

                def self.available_agents_with_concurrent(chat_ids, diff = 2.minutes)
                  agents = {}
                  Chat::Agent.where(active: true).where('updated_at > ?', Time.zone.now - diff).each do |record|
                    user = User.lookup(id: record.updated_by_id)
                    next if !user
              Severity: Minor
              Found in app/models/chat.rb - About 25 mins to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              There are no issues that match your filters.

              Category
              Status